package com.iplant.aps.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.iplant.aps.service.MyHelperService;
import com.iplant.aps.service.mesenum.*;
import com.iplant.aps.service.po.APIResult;
import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.po.Pagination;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.andon.FocasLGL;
import com.iplant.aps.service.po.aps.*;
import com.iplant.aps.service.po.bfc.BFCMessage;
import com.iplant.aps.service.po.bms.BMSDepartment;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.bms.BMSPosition;
import com.iplant.aps.service.po.bms.BMSWorkCharge;
import com.iplant.aps.service.po.bpm.BPMActivitiHisTask;
import com.iplant.aps.service.po.bpm.BPMForm;
import com.iplant.aps.service.po.bpm.BPMOperationStep;
import com.iplant.aps.service.po.excel.ExcelData;
import com.iplant.aps.service.po.excel.ExcelLineData;
import com.iplant.aps.service.po.excel.ExcelSheetData;
import com.iplant.aps.service.po.excel.MyExcelSheet;
import com.iplant.aps.service.po.fmc.FMCLine;
import com.iplant.aps.service.po.fmc.FMCWorkCharge;
import com.iplant.aps.service.po.fpc.*;
import com.iplant.aps.service.po.ipt.IPTOutsourcingMaterial;
import com.iplant.aps.service.po.lfs.LFSOperationLog;
import com.iplant.aps.service.po.lfs.LFSWorkAreaChecker;
import com.iplant.aps.service.po.lfs.LFSWorkAreaStation;
import com.iplant.aps.service.po.mrp.MRPMaterialPlan;
import com.iplant.aps.service.po.mss.MSSMaterial;
import com.iplant.aps.service.po.oms.OMSCheckMsg;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.service.po.rsm.RSMTurnOrderDesc;
import com.iplant.aps.service.po.rsm.RSMTurnOrderTask;
import com.iplant.aps.service.po.sfc.SFCBOMTask;
import com.iplant.aps.service.po.sfc.SFCBOMTaskItem;
import com.iplant.aps.service.po.sfc.SFCTaskIPT;
import com.iplant.aps.service.po.sfc.SFCTaskStep;
import com.iplant.aps.service.po.wms.*;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import com.iplant.aps.serviceimpl.dao.aps.APSBOMItemDAO;
import com.iplant.aps.serviceimpl.dao.aps.APSBomBPMItemDAO;
import com.iplant.aps.serviceimpl.dao.aps.APSSchedulingVersionBPMDAO;
import com.iplant.aps.serviceimpl.dao.aps.APSTaskPartDAO;
import com.iplant.aps.serviceimpl.dao.aps.APSTaskStepDAO;
import com.iplant.aps.serviceimpl.dao.bfc.BFCMessageDAO;
import com.iplant.aps.serviceimpl.dao.ipt.IPTOutsourcingMaterialDAO;
import com.iplant.aps.serviceimpl.dao.mes.MESSystemConfigDAO;
import com.iplant.aps.serviceimpl.dao.mrp.MRPMaterialPlanDAO;
import com.iplant.aps.serviceimpl.dao.oms.OMSCheckMsgDAO;
import com.iplant.aps.serviceimpl.dao.oms.OMSOrderDAO;
import com.iplant.aps.serviceimpl.dao.rsm.RSMTurnOrderDescDAO;
import com.iplant.aps.serviceimpl.dao.sfc.SFCBOMTaskDAO;
import com.iplant.aps.serviceimpl.dao.sfc.SFCBOMTaskItemDAO;
import com.iplant.aps.serviceimpl.dao.sfc.SFCTaskStepDAO;
import com.iplant.aps.serviceimpl.dao.wms.MESMaterialReturnDAO;
import com.iplant.aps.serviceimpl.dao.wms.WMSPickDemandDAO;
import com.iplant.aps.serviceimpl.dao.wms.WMSPickDemandItemDAO;
import com.iplant.aps.serviceimpl.dao.wms.WMSPickDemandItemStockDAO;
import com.iplant.aps.serviceimpl.utils.MESServer;
import com.iplant.aps.serviceimpl.utils.aps.APSConstans;
import com.iplant.aps.serviceimpl.utils.aps.APSUtils;
import com.iplant.aps.shristool.CalendarDAO;
import com.iplant.aps.utils.RemoteInvokeUtils;
import com.iplant.aps.utils.aps.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author PengYouWang
 * 2020-4-1 13:53:36
 */
@Service
public class MyHelperServiceImpl implements MyHelperService {

    private static final Logger logger = LoggerFactory.getLogger(MyHelperServiceImpl.class);

    public MyHelperServiceImpl() {
    }

    private static MyHelperService Instance;

    public static MyHelperService getInstance() {
        if (Instance == null)
            Instance = new MyHelperServiceImpl();
        return Instance;
    }

    @Override
    public ServiceResult<Map<String, FMCLine>> FMC_QueryLineNameMap(BMSEmployee wLoginUser) {
        ServiceResult<Map<String, FMCLine>> wResult = new ServiceResult<>();
        try {
            wResult.Result = new HashMap<>();

            List<FMCLine> wLineList = CoreServiceImpl.getInstance().FMC_QueryLineList(wLoginUser).List(FMCLine.class);
            if (wLineList == null || wLineList.size() <= 0)
                return wResult;

            for (FMCLine wFMCLine : wLineList) {
                if (wResult.Result.containsKey(wFMCLine.Name))
                    continue;
                wResult.Result.put(wFMCLine.Name, wFMCLine);
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Map<String, FPCPart>> FPC_QueryPartNameMap(BMSEmployee wLoginUser) {
        ServiceResult<Map<String, FPCPart>> wResult = new ServiceResult<>();
        try {
            wResult.Result = new HashMap<>();

            List<FPCPart> wList = CoreServiceImpl.getInstance().FPC_QueryPartList(wLoginUser).List(FPCPart.class);
            if (wList == null || wList.size() <= 0)
                return wResult;

            for (FPCPart wFPCPart : wList) {
                if (wResult.Result.containsKey(wFPCPart.Name))
                    continue;
                wResult.Result.put(wFPCPart.Name, wFPCPart);
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Map<String, FPCPartPoint>> FPC_QueryPartPointNameMap(BMSEmployee wLoginUser) {
        ServiceResult<Map<String, FPCPartPoint>> wResult = new ServiceResult<>();
        try {
            List<FPCPartPoint> wList = CoreServiceImpl.getInstance().FPC_QueryPartPointList(wLoginUser)
                    .List(FPCPartPoint.class);
            if (wList == null || wList.size() <= 0)
                return wResult;

            for (FPCPartPoint wFPCPartPoint : wList) {
                if (wResult.Result.containsKey(wFPCPartPoint.Name))
                    continue;
                wResult.Result.put(wFPCPartPoint.Name, wFPCPartPoint);
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Map<Integer, FMCWorkCharge>> FMC_QueryWorkChargeMap(BMSEmployee wLoginUser) {
        ServiceResult<Map<Integer, FMCWorkCharge>> wResult = new ServiceResult<>();
        try {
            wResult.Result = new HashMap<>();

            List<FMCWorkCharge> wList = CoreServiceImpl.getInstance().FMC_QueryWorkChargeList(wLoginUser)
                    .List(FMCWorkCharge.class);
            if (wList == null || wList.size() <= 0) {
                return wResult;
            }

            for (FMCWorkCharge wFMCWorkCharge : wList) {
                if (wResult.Result.containsKey(wFMCWorkCharge.StationID)) {
                    continue;
                }
                wResult.Result.put(wFMCWorkCharge.StationID, wFMCWorkCharge);
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Map<String, FPCProduct>> FPC_QueryProductNameMap(BMSEmployee wLoginUser) {
        ServiceResult<Map<String, FPCProduct>> wResult = new ServiceResult<>();
        try {
            wResult.Result = new HashMap<>();

            APIResult wApiResult = CoreServiceImpl.getInstance().FPC_QueryProductList(wLoginUser, -1, -1);
            List<FPCProduct> wFPCProductList = wApiResult.List(FPCProduct.class);
            if (wFPCProductList == null || wFPCProductList.size() <= 0)
                return wResult;

            wFPCProductList = wFPCProductList.stream().filter(p -> p.Active == 1).collect(Collectors.toList());

            for (FPCProduct wFPCProduct : wFPCProductList) {
                if (!wResult.Result.containsKey(wFPCProduct.ProductNo)) {
                    wResult.Result.put(wFPCProduct.ProductNo, wFPCProduct);
                }
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Map<Integer, List<Integer>>> LFS_QueryWorkAreaCheckListMap(BMSEmployee wLoginUser) {
        ServiceResult<Map<Integer, List<Integer>>> wResult = new ServiceResult<>();
        try {
            wResult.Result = new HashMap<>();

            List<LFSWorkAreaChecker> wList = LFSServiceImpl.getInstance().LFS_QueryWorkAreaCheckerList(wLoginUser)
                    .List(LFSWorkAreaChecker.class);
            if (wList == null || wList.size() <= 0)
                return wResult;

            List<LFSWorkAreaChecker> wTempList = null;
            List<Integer> wIDList = null;
            for (LFSWorkAreaChecker wLFSWorkAreaChecker : wList) {
                if (wResult.Result.containsKey(wLFSWorkAreaChecker.WorkAreaID))
                    continue;

                wTempList = wList.stream().filter(p -> p.WorkAreaID == wLFSWorkAreaChecker.WorkAreaID)
                        .collect(Collectors.toList());
                if (wTempList.size() > 0) {
                    wIDList = new ArrayList<>();
                    for (LFSWorkAreaChecker wItem : wTempList) {
                        wIDList.addAll(wItem.CheckerIDList);
                    }
                    wResult.Result.put(wLFSWorkAreaChecker.WorkAreaID, wIDList);
                }
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<Integer>> FPC_QueryPreStationIDList(BMSEmployee wLoginUser, int wOrderID,
                                                                  int wStationID) {
        ServiceResult<List<Integer>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>();
            wResult.Result = new ArrayList<>();
            OMSOrder wOMSOrder = OMSOrderDAO.getInstance().SelectByID(wLoginUser, wOrderID, wErrorCode);
            if (wOMSOrder == null || wOMSOrder.ID <= 0) {
                return wResult;
            }

            int wRouteID = Math.max(APSConstans.GetFPCRoute(wOMSOrder.ProductID, wOMSOrder.LineID,
                    wOMSOrder.CustomerID).ID, 0);
            if (wRouteID <= 0) {
                return wResult;
            }

            List<FPCRoutePart> wPartList = CoreServiceImpl.getInstance().FPC_QueryRoutePartList(wLoginUser, wRouteID)
                    .List(FPCRoutePart.class);

            if (wPartList == null || wPartList.size() <= 0) {
                return wResult;
            }
            // 直接上级工位
            Optional<FPCRoutePart> wRoutePart = wPartList.stream().filter(p -> p.PartID == wStationID).findFirst();
            wRoutePart.ifPresent(fpcRoutePart -> wResult.Result.add(fpcRoutePart.PrevPartID));
            // 间接上级工位
            List<FPCRoutePart> wTempList = wPartList.stream()
                    .filter(p -> p.NextPartIDMap.containsKey(String.valueOf(wStationID))).collect(Collectors.toList());
            if (wTempList.size() > 0) {
                wResult.Result.addAll(wTempList.stream().map(p -> p.PartID).collect(Collectors.toList()));
            }

            // 去除为0的工位ID
            if (wResult.Result.size() > 0) {
                wResult.Result.removeIf(p -> p <= 0);
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Boolean> SFC_JudgeTaskStepIsCanDo(BMSEmployee wLoginUser, APSTaskStep wAPSTaskStep) {
        ServiceResult<Boolean> wResult = new ServiceResult<>();
        try {
            // 查询上工位ID集合
            List<Integer> wPreStationIDList = this.FPC_QueryPreStationIDList(wLoginUser, wAPSTaskStep.OrderID,
                    wAPSTaskStep.PartID).Result;
            if (wPreStationIDList.size() <= 0) {
                wResult.Result = true;
            } else {
                List<RSMTurnOrderTask> wTempList = QMSServiceImpl.getInstance()
                        .RSM_QueryTurnOrderTaskList(wLoginUser, wAPSTaskStep.OrderID, -1, wAPSTaskStep.PartID,
                                new ArrayList<>(Collections.singletonList(SFCTurnOrderTaskStatus.Passed.getValue())))
                        .List(RSMTurnOrderTask.class);
                if (wTempList == null || wTempList.size() <= 0) {
                    wResult.Result = false;
                } else {
                    wResult.Result = wPreStationIDList.stream()
                            .allMatch(p -> wTempList.stream().anyMatch(q -> q.ApplyStationID == p));
                }
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Boolean> WDW_IsSendRepair(BMSEmployee wAdminUser, int taskID, int iPTItemID) {
        ServiceResult<Boolean> wResult = new ServiceResult<>();
        try {
            Map<String, Integer> wMap = WDWServiceImpl.getInstance().WDW_SpecialItemAll_Repair(wAdminUser, taskID)
                    .Info(Map.class);
            if (wMap == null || wMap.size() <= 0) {
                wResult.Result = false;
            } else if (wMap.containsKey(String.valueOf(iPTItemID))) {
                wResult.Result = true;
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public List<FocasLGL> Focas_LGLMES(BMSEmployee wLoginUser, int wYear) {
        List<FocasLGL> wResult = new ArrayList<>();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("y", wYear);

            String wUrl = StringUtils.Format(
                    "http://10.200.10.19:8080/Portal/Statistics/LGLMES?cadv_ao={0}&cade_po={1}&company_id={2}",
                    wLoginUser.LoginName, wLoginUser.Password, wLoginUser.CompanyID);

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.GET,
                    HashMap.class);
            wResult = CloneTool.CloneArray(wMap.get("data"), FocasLGL.class);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_PostLL(WMSLinePartLLs wWMSLinePartLLs, WMSPickDemand wPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wWMSLinePartLLs);

            // WMS正式网址
            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=OUB_02";

            // 产线领料出库下发接口
            if (StringUtils.isNotEmpty(APSConstans.LineLLURL)) {
                wUrl = APSConstans.LineLLURL;
            }

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            OutResult<Integer> wErrorCode = new OutResult<>(0);
            String wLog1 = JSON.toJSONString(wParms);
            String wLog2 = JSON.toJSONString(wMap);
            String wLog = StringUtils.Format("{0}::{1}", wLog1, wLog2);
            OMSOrder wOrder = OMSOrderDAO.getInstance().SelectByID(BaseDAO.SysAdmin, wPickDemand.OrderID, wErrorCode);
            CalendarDAO.getInstance().WriteLogFile(wLog, "领料需求WMS推送", wOrder, wPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_SynchronizTime(WMSLinePartLLs wWMSLinePartLLs, WMSPickDemand wPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wWMSLinePartLLs);

            // WMS正式网址
            String wUrl = APSConstans.wmsIp + Configuration.readConfigString("WMSDemandUpdateUrl", "config/config");
            if (StringUtils.isNotEmpty(APSConstans.OrderUpdateURL)) {
                wUrl = APSConstans.OrderUpdateURL;
            }

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            OutResult<Integer> wErrorCode = new OutResult<>(0);
            String wLog1 = JSON.toJSONString(wParms);
            String wLog2 = JSON.toJSONString(wMap);
            String wLog = StringUtils.Format("{0}::{1}", wLog1, wLog2);
            OMSOrder wOrder = OMSOrderDAO.getInstance().SelectByID(BaseDAO.SysAdmin, wPickDemand.OrderID, wErrorCode);
            CalendarDAO.getInstance().WriteLogFile(wLog, "领料需求WMS修改", wOrder, wPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_PostLL(WMSLinePartLLs wWMSLinePartLLs, WMSBatchPickDemand wPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wWMSLinePartLLs);

            // WMS正式网址
            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=OUB_02";

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            OutResult<Integer> wErrorCode = new OutResult<>(0);
            String wLog1 = JSON.toJSONString(wParms);
            String wLog2 = JSON.toJSONString(wMap);
            String wLog = StringUtils.Format("{0}::{1}", wLog1, wLog2);
            CalendarDAO.getInstance().WriteLogFile(wLog, "领料需求WMS推送", new OMSOrder(), wPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public void WMS_TestSend() {
        try {
            Map<String, Object> wParms = new HashMap<>();

            Map<String, String> wCustom = new HashMap<>();
            wCustom.put("appId", "83e90e28cc6");
            wCustom.put("secretKey", "ua3Bhy9pv3ee9jfJSwNgjVVstvLzbrb8");
            wCustom.put("appUrl", "/MESCore/my_app/contain.html?username=012100090015");
            wParms.put("custom", wCustom);
            wParms.put("userAccountList", "012100090015");
            wParms.put("title", "通知标题3");
            wParms.put("content", "通知文本3");
            wParms.put("appName", "com.crrcgzgs.portal");
            wParms.put("insideAppName", "mes");
            wParms.put("type", 1);

            logger.info(JSON.toJSONString(wParms));

            String wUrl = "http://10.200.3.7:8081/gxhspush/platform/platFormJsonPushMsg.act";

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);

            logger.info(JSON.toJSONString(wMap));
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    @Override
    public WMSReturn WMS_PostLLCancel(WMSPickDemand wWMSPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            WMSLinePartLLCancel wWMSLinePartLLCancel = WMSServiceImpl.getInstance()
                    .GetWMSLinePartLLCancel(wWMSPickDemand);

            Map<String, Object> wOrderNoMap = new HashMap<>();
            wOrderNoMap.put("ordernos", wWMSLinePartLLCancel);

            Map<String, Object> wDataMap = new HashMap<>();
            wDataMap.put("data", wOrderNoMap);

            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wDataMap);

            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=OUB08";
            if (StringUtils.isNotEmpty(APSConstans.OrderCancelURL)) {
                wUrl = APSConstans.OrderCancelURL;
            }

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            OutResult<Integer> wErrorCode = new OutResult<>(0);
            OMSOrder wOrder = OMSOrderDAO.getInstance().SelectByID(BaseDAO.SysAdmin, wWMSPickDemand.OrderID,
                    wErrorCode);
            CalendarDAO.getInstance().WriteLogFile(JSON.toJSONString(wParms) + "::" + JSON.toJSONString(wResult),
                    "WMS产线领料取消下发", wOrder, wWMSPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_PostLLCancel(WMSBatchPickDemand wWMSBatchPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            WMSLinePartLLCancel wWMSLinePartLLCancel = WMSServiceImpl.getInstance()
                    .GetWMSLinePartLLCancel(wWMSBatchPickDemand);

            Map<String, Object> wOrderNoMap = new HashMap<>();
            wOrderNoMap.put("ordernos", wWMSLinePartLLCancel);

            Map<String, Object> wDataMap = new HashMap<>();
            wDataMap.put("data", wOrderNoMap);

            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wDataMap);

            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=OUB08";

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            CalendarDAO.getInstance().WriteLogFile(JSON.toJSONString(wParms) + "::" + JSON.toJSONString(wResult),
                    "WMS产线领料取消下发", new OMSOrder(), wWMSBatchPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_PostTL(WMSLinePartTLs wWMSLinePartTLs, MESMaterialReturn wData, OMSOrder wOrder) {
        WMSReturn wResult = new WMSReturn();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wWMSLinePartTLs);

            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=INB07";

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            String wms_line_return_material = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "WMS_Line_Return_Material", wErrorCode);
            if (StringUtils.isNotEmpty(wms_line_return_material)) {
                wUrl = wms_line_return_material;
            }

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            CalendarDAO.getInstance().WriteLogFile(JSON.toJSONString(wParms) + "::" + JSON.toJSONString(wResult),
                    "WMS产线退料", wOrder, wData.Code);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_MPSDisassembly(WMSSubRepairReceipts wWMSSubRepairReceipts, WMSPickDemand wPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wWMSSubRepairReceipts);

            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=INB09";

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            String wms_out_repair_demand_send = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "WMS_Out_Repair_Demand_Send", wErrorCode);
            if (StringUtils.isNotEmpty(wms_out_repair_demand_send)) {
                wUrl = wms_out_repair_demand_send;
            }

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            // OutResult<Integer> wErrorCode = new OutResult<>(0);
            String wLog1 = JSON.toJSONString(wParms);
            String wLog2 = JSON.toJSONString(wMap);
            String wLog = StringUtils.Format("{0}::{1}", wLog1, wLog2);
            OMSOrder wOrder = OMSOrderDAO.getInstance().SelectByID(BaseDAO.SysAdmin, wPickDemand.OrderID, wErrorCode);
            CalendarDAO.getInstance().WriteLogFile(wLog, "委外拆修入库WMS推送", wOrder, wPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public WMSReturn WMS_MPSDisassembly_1(WMSSubRepairReceipts wWMSSubRepairReceipts, WMSPickDemand wPickDemand) {
        WMSReturn wResult = new WMSReturn();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("xmldata", wWMSSubRepairReceipts);

            String wUrl = APSConstans.wmsIp
                    + "/datahub/gzloco/FluxWmsJsonApi/?client_customerid=FLUXWMSGJCMES&messageid=INB09";

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);
            wResult = CloneTool.Clone(((Map<String, Object>) wMap.get("Response")).get("return"), WMSReturn.class);

            OutResult<Integer> wErrorCode = new OutResult<>(0);
            String wLog1 = JSON.toJSONString(wParms);
            String wLog2 = JSON.toJSONString(wMap);
            String wLog = StringUtils.Format("{0}::{1}", wLog1, wLog2);
            OMSOrder wOrder = OMSOrderDAO.getInstance().SelectByID(BaseDAO.SysAdmin, wPickDemand.OrderID, wErrorCode);
            CalendarDAO.getInstance().WriteLogFile(wLog, "自修件入库WMS推送", wOrder, wPickDemand.DemandNo);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_TriggerSelfRepairPartsWarehousingTask(BMSEmployee wLoginUser,
                                                                            APSTaskStep wAPSTaskStep) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (StringUtils.isEmpty(wAPSTaskStep.MaterialNoNew)) {
                return wResult;
            }

            OMSOrder wOMSOrder = OMSOrderDAO.getInstance().SelectByID(wLoginUser, wAPSTaskStep.OrderID, wErrorCode);

            List<MSSMaterial> wMSSMaterialList = WMSServiceImpl.getInstance().MSS_QueryMaterialList(wLoginUser,
                    wAPSTaskStep.MaterialNoNew).List(MSSMaterial.class);
            if (wMSSMaterialList == null || wMSSMaterialList.size() <= 0) {
                return wResult;
            }

            MSSMaterial wMSSMaterial = wMSSMaterialList.get(0);

            // ①创建入库单
            // 创建送修单
            String monitorNo = "";
            String monitor = "";
            BMSEmployee wUser = WMSPickDemandDAO.getInstance().GetMonitorByPart(wLoginUser, wAPSTaskStep.PartID,
                    wErrorCode);
            if (StringUtils.isNotEmpty(wUser.LoginID)) {
                monitorNo = wUser.LoginID;
                monitor = wUser.Name;
            }

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);
            String wCode = WMSPickDemandDAO.getInstance().GetNewCode(wLoginUser, wErrorCode);
            WMSPickDemand wWMSPickDemand = new WMSPickDemand(0, "1900",
                    String.valueOf(WMSOrderType.SelfRepairInstock.getValue()), wCode, Calendar.getInstance(),
                    Calendar.getInstance(), monitorNo, monitor, wOMSOrder.ProductID, wOMSOrder.ProductNo,
                    wOMSOrder.LineID, wOMSOrder.LineName, wOMSOrder.CustomerID,
                    wOMSOrder.Customer,
                    APSConstans.GetCRMCustomer(wOMSOrder.CustomerID).CustomerCode, wOMSOrder.ID,
                    wOMSOrder.PartNo, wAPSTaskStep.PartID, APSConstans.GetFPCPartName(wAPSTaskStep.PartID),
                    APSConstans.GetFPCPart(wAPSTaskStep.PartID).Code, "", "", wBaseTime, "", "", wBaseTime, 8,
                    wLoginUser.ID, wLoginUser.Name, Calendar.getInstance(), wOMSOrder.OrderNo, wBaseTime);
            int wDemandID = WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
            if (wDemandID > 0) {
                wWMSPickDemand.ID = wDemandID;
                // ④创建领料需求单明细
                WMSPickDemandItem wWMSPickDemandItem = new WMSPickDemandItem(0, wDemandID,
                        wMSSMaterial.ID, wMSSMaterial.MaterialNo,
                        wMSSMaterial.MaterialName, wAPSTaskStep.FQTY,
                        wOMSOrder.PartNo, wAPSTaskStep.StepID,
                        APSConstans.GetFPCPartPoint(wAPSTaskStep.StepID).Code.replace("PS-", ""),
                        APSConstans.GetFPCPartPointName(wAPSTaskStep.StepID), "1", "",
                        0, "", APSOutSourceType.ZXBX.getValue(),
                        APSOutSourceType.ZXBX.getLable(), "", "",
                        wBaseTime, wWMSPickDemand.PartID, 1, wAPSTaskStep.FQTY, 0);
                // wWMSPickDemandItem.IsOriDis=wMRPMaterialPlan.OriginalType==1?"是":"否";

                WMSPickDemandItemDAO.getInstance().Update(wLoginUser, wWMSPickDemandItem, wErrorCode);
                // ②发送待办消息给相关人员完成配送交接
                SendMessageToJJ(wLoginUser, wMSSMaterial, wCode, wDemandID, wOMSOrder, wAPSTaskStep, wWMSPickDemand);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 发送待办消息给相关人员完成配送交接
     */
    private void SendMessageToJJ(BMSEmployee wLoginUser, MSSMaterial wMSSMaterial, String wCode, int wDemandID,
                                 OMSOrder wOMSOrder, APSTaskStep wAPSTaskStep, WMSPickDemand wWMSPickDemand) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            // 查询自修件的组装工位
            int wPartID = IPTOutsourcingMaterialDAO.getInstance().IPT_QuerySelfZZPartID(wLoginUser, wOMSOrder.RouteID,
                    wMSSMaterial.MaterialNo, wErrorCode);

            // 查询工位配送员
            if (wPartID > 0) {
                FPCPart wFPCPart = APSConstans.GetFPCPart(wPartID);
                if (wFPCPart.DistributorList != null && wFPCPart.DistributorList.size() > 0) {
                    wWMSPickDemand.DeliveryID = wFPCPart.DistributorList.get(0);

                    List<BFCMessage> wBFCMessageList = new ArrayList<>();
                    BFCMessage wMessage = null;
                    int wShiftID = MESServer.MES_QueryShiftID(wLoginUser.CompanyID, Calendar.getInstance(),
                            APSShiftPeriod.Day, FMCShiftLevel.Day, 0);
                    // 发送任务消息到人员
                    wMessage = new BFCMessage();
                    wMessage.Active = 0;
                    wMessage.CompanyID = 0;
                    wMessage.CreateTime = Calendar.getInstance();
                    wMessage.EditTime = Calendar.getInstance();
                    wMessage.ID = 0;
                    wMessage.MessageID = wDemandID;
                    wMessage.Title = wCode;
                    wMessage.MessageText = StringUtils.Format("【{0}】 {1}完成了【{2}】自修件的维修作业，请及时配送交接。",
                            BPMEventModule.SelfRepairInstock.getLable(), wLoginUser.Name, StringUtils.Format("{0}-{1}",
                                    wMSSMaterial.MaterialNo, wMSSMaterial.MaterialName));
                    wMessage.ModuleID = BPMEventModule.SelfRepairInstock.getValue();
                    wMessage.ResponsorID = wFPCPart.DistributorList.get(0);
                    wMessage.ShiftID = wShiftID;
                    wMessage.StationID = 0;
                    wMessage.Type = BFCMessageType.Task.getValue();
                    wBFCMessageList.add(wMessage);
                    CoreServiceImpl.getInstance().BFC_UpdateMessageList(wLoginUser, wBFCMessageList);
                }
            }

            if (wPartID > 0) {
                FPCPart wSelfPart = APSConstans.GetFPCPart(wPartID);
                if (wSelfPart.DepartmentIDList != null && wSelfPart.DepartmentIDList.size() > 0) {
                    List<BMSEmployee> wMonitorList = APSConstans.GetBMSEmployeeList().values().stream()
                            .filter(p -> p.DepartmentID == wSelfPart.DepartmentIDList.get(0)
                                    && APSConstans.GetBMSPosition(p.Position).DutyID == 1)
                            .collect(Collectors.toList());
                    if (wMonitorList.size() > 0) {
                        wWMSPickDemand.ReceiveID = wMonitorList.get(0).ID;

                        List<BFCMessage> wBFCMessageList = new ArrayList<>();
                        BFCMessage wMessage = null;
                        int wShiftID = MESServer.MES_QueryShiftID(wLoginUser.CompanyID, Calendar.getInstance(),
                                APSShiftPeriod.Day, FMCShiftLevel.Day, 0);
                        // 发送任务消息到人员
                        wMessage = new BFCMessage();
                        wMessage.Active = 0;
                        wMessage.CompanyID = 0;
                        wMessage.CreateTime = Calendar.getInstance();
                        wMessage.EditTime = Calendar.getInstance();
                        wMessage.ID = 0;
                        wMessage.MessageID = wDemandID;
                        wMessage.Title = wCode;
                        wMessage.MessageText = StringUtils.Format("【{0}】 {1}完成了【{2}】自修件的维修作业，请及时与配送班交接。",
                                BPMEventModule.SelfRepairRKJJ.getLable(), wLoginUser.Name, StringUtils.Format("{0" +
                                                "}-{1}",
                                        wMSSMaterial.MaterialNo, wMSSMaterial.MaterialName));
                        wMessage.ModuleID = BPMEventModule.SelfRepairRKJJ.getValue();
                        wMessage.ResponsorID = wMonitorList.get(0).ID;
                        wMessage.ShiftID = wShiftID;
                        wMessage.StationID = 0;
                        wMessage.Type = BFCMessageType.Task.getValue();
                        wBFCMessageList.add(wMessage);
                        CoreServiceImpl.getInstance().BFC_UpdateMessageList(wLoginUser, wBFCMessageList);
                    }
                }
            }
            // 保存配送员、接收员
            WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    @Override
    public ServiceResult<List<WMSPickDemand>> WMS_QueryEmployeeAll_SelfTask(BMSEmployee wLoginUser, int wTagType,
                                                                            Calendar wStartTime, Calendar wEndTime) {
        ServiceResult<List<WMSPickDemand>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wTagType == 1) {

                List<Integer> wZSCodeIDList = BFCMessageDAO.getInstance().BFC_QueryMessageIDToDoList(wLoginUser,
                        BPMEventModule.SelfRepairInstock.getValue(), wErrorCode);
                List<Integer> wTaskIDList = new ArrayList<>(wZSCodeIDList);

                List<Integer> wSMCodeIDList = BFCMessageDAO.getInstance().BFC_QueryMessageIDToDoList(wLoginUser,
                        BPMEventModule.SelfRepairRKJJ.getValue(), wErrorCode);
                wTaskIDList.addAll(wSMCodeIDList);

                if (wTaskIDList.size() > 0) {
                    wResult.Result = WMSPickDemandDAO.getInstance().SelectList(wLoginUser, -1, "", "", -1, -1, -1, "",
                            -1, -1, null, null, wTaskIDList, wErrorCode);
                    for (WMSPickDemand wWMSPickDemand : wResult.Result) {
                        if (wZSCodeIDList.stream().anyMatch(p -> p == wWMSPickDemand.ID)) {
                            wWMSPickDemand.TagType = 1;
                        } else if (wSMCodeIDList.stream().anyMatch(p -> p == wWMSPickDemand.ID)) {
                            wWMSPickDemand.TagType = 2;
                        } else {
                            wWMSPickDemand.TagType = 0;
                        }
                    }
                }
            } else if (wTagType == 2) {
                List<Integer> wTaskIDList = BFCMessageDAO.getInstance().BFC_QueryMessageIDDoneList(wLoginUser,
                        BPMEventModule.SelfRepairInstock.getValue(), wStartTime, wEndTime, wErrorCode);
                wTaskIDList.addAll(BFCMessageDAO.getInstance().BFC_QueryMessageIDDoneList(wLoginUser,
                        BPMEventModule.SelfRepairRKJJ.getValue(), wStartTime, wEndTime, wErrorCode));
                if (wTaskIDList.size() > 0) {
                    wResult.Result = WMSPickDemandDAO.getInstance().SelectList(wLoginUser, -1, "", "", -1, -1, -1, "",
                            -1, -1, null, null, wTaskIDList, wErrorCode);
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SelfTaskApplyInstock(BMSEmployee wLoginUser, WMSPickDemand wPickDemand) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // 保存子项
            for (WMSPickDemandItem wWMSPickDemandItem : wPickDemand.ItemList) {
                WMSPickDemandItemDAO.getInstance().Update(wLoginUser, wWMSPickDemandItem, wErrorCode);
            }

            // 手动推送给wms
            List<WMSSubRepairReceipt> wHeaderList = new ArrayList<>();
            WMSSubRepairReceipt wWMSSubRepairReceipt = GetWMSSubRepairReceipt(wPickDemand);
            wHeaderList.add(wWMSSubRepairReceipt);
            WMSSubRepairReceipts wWMSSubRepairReceipts = new WMSSubRepairReceipts(wHeaderList);
            WMSReturn wReturn = new WMSReturn();

            if (wReturn.returnFlag.equals("1")) {
                wPickDemand.Status = WMSPickDemandStatus.ToJJ.getValue();
                wPickDemand.SendStatus = 1;
                wPickDemand.SendDesc = "推送成功";

                // 关闭入库申请消息
                CoreServiceImpl.getInstance().BFC_MsgHandleTask(wLoginUser,
                        BPMEventModule.SelfRepairInstock.getValue(), 0, 3,
                        new ArrayList<>(Collections.singletonList(wPickDemand.ID)), wLoginUser.ID, 1);

                // 发送待办消息给申请人进行委外拆修入库交接
                List<BFCMessage> wBFCMessageList = new ArrayList<>();
                BFCMessage wMessage = null;
                SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
                int wShiftID = Integer.parseInt(wSDF.format(Calendar.getInstance().getTime()));

                List<Integer> wUserIDList = new ArrayList<>(Collections.singletonList(wPickDemand.CreateID));
                String wMessageText = StringUtils.Format("【{0}】-【{2}】-【{3}】 {1}已发起自修件入库申请，请及时完成入库交接。",
                        BPMEventModule.SelfRepairRKJJ.getLable(), wLoginUser.Name, wPickDemand.PartNo,
                        wPickDemand.PartName);

                for (int wUserID : wUserIDList) {
                    // 发送任务消息到人员
                    wMessage = new BFCMessage();
                    wMessage.Active = 0;
                    wMessage.CompanyID = 0;
                    wMessage.CreateTime = Calendar.getInstance();
                    wMessage.EditTime = Calendar.getInstance();
                    wMessage.ID = 0;
                    wMessage.MessageID = wPickDemand.ID;
                    wMessage.Title = wPickDemand.DemandNo;
                    wMessage.MessageText = wMessageText;
                    wMessage.ModuleID = BPMEventModule.SelfRepairRKJJ.getValue();
                    wMessage.ResponsorID = wUserID;
                    wMessage.ShiftID = wShiftID;
                    wMessage.StationID = 0;
                    wMessage.Type = BFCMessageType.Task.getValue();
                    wBFCMessageList.add(wMessage);
                }

                CoreServiceImpl.getInstance().BFC_UpdateMessageList(BaseDAO.SysAdmin, wBFCMessageList);
            } else {
                wPickDemand.SendStatus = 2;

                if (StringUtils.isEmpty(wReturn.returnDesc))
                    wPickDemand.SendDesc = "网络错误";
                else
                    wPickDemand.SendDesc = wReturn.returnDesc;

                wResult.FaultCode += wReturn.returnDesc;
            }
            WMSPickDemandDAO.getInstance().Update(wLoginUser, wPickDemand, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 获取待推送的自修件入库数据
     */
    private WMSSubRepairReceipt GetWMSSubRepairReceipt(WMSPickDemand wPickDemand) {
        WMSSubRepairReceipt wResult = new WMSSubRepairReceipt();
        try {
            wResult.warehouseId = "WH01";
            wResult.customerId = "1900";
            wResult.asnType = "200";
            wResult.docNo = wPickDemand.DemandNo;
            wResult.expectedArriveTime1 = wPickDemand.ExpectTime1Text;
            wResult.expectedArriveTime2 = wPickDemand.ExpectTime2Text;
            wResult.supplierId = wPickDemand.MonitorNo;
            wResult.supplierName = wPickDemand.Monitor;
            wResult.hedi01 = wPickDemand.ProductNo;
            wResult.hedi02 = wPickDemand.LineName;
            wResult.hedi03 = wPickDemand.CustomerCode;
            wResult.hedi04 = wPickDemand.PartNo;
            wResult.hedi05 = wPickDemand.PartName;
            wResult.hedi06 = wPickDemand.PartCode;
            wResult.details = new ArrayList<>();

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyy-MM-dd");
            for (WMSPickDemandItem wItem : wPickDemand.ItemList) {
                WMSSubRepairReceiptDetail wWMSSubRepairReceiptDetail = new WMSSubRepairReceiptDetail();
                wWMSSubRepairReceiptDetail.sku = wItem.MaterialNo;
                wWMSSubRepairReceiptDetail.expectedQty = String.valueOf(wItem.FQTY);
                wWMSSubRepairReceiptDetail.lotAtt01 = wSDF.format(wItem.ProductTime.getTime());
                wWMSSubRepairReceiptDetail.lotAtt02 = wSDF.format(Calendar.getInstance().getTime());
                wWMSSubRepairReceiptDetail.lotAtt03 = wSDF.format(Calendar.getInstance().getTime());
                wWMSSubRepairReceiptDetail.lotAtt04 = wItem.Stock;
                wWMSSubRepairReceiptDetail.lotAtt05 = wItem.Supplier;
                wWMSSubRepairReceiptDetail.lotAtt08 = wItem.WBSNo.substring(0, 9);
                wWMSSubRepairReceiptDetail.lotAtt09 = wItem.BatchNumber;
                wWMSSubRepairReceiptDetail.lotAtt10 = wItem.SerialNo;
                wWMSSubRepairReceiptDetail.lotAtt11 = wItem.AssessmentType;
                wWMSSubRepairReceiptDetail.dedi01 = wItem.PartPointCode;
                wWMSSubRepairReceiptDetail.dedi02 = wItem.PartPointName;
                wWMSSubRepairReceiptDetail.dedi03 = wItem.RowNo;
                wResult.details.add(wWMSSubRepairReceiptDetail);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<String> WMS_ShowZXQRCode(BMSEmployee wLoginUser, int wDemandID) {
        ServiceResult<String> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            WMSPickDemand wItem = WMSPickDemandDAO.getInstance().SelectByID(wLoginUser, wDemandID, wErrorCode);

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());
            String wTextMain = StringUtils.Format("{0},{1},{2},{3}", wItem.DemandNo, wItem.PartCode, wLoginUser.LoginID,
                    wCurrentTime);
            wResult.Result = APSUtils.getInstance().getQRCodeString(wLoginUser, wTextMain);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_ZXJDisassembly(BMSEmployee wLoginUser, int wDemandID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            WMSPickDemand wPickDemand = WMSPickDemandDAO.getInstance().SelectByID(wLoginUser, wDemandID, wErrorCode);

            // 手动推送给wms
            List<WMSSubRepairReceipt> wHeaderList = new ArrayList<>();
            WMSSubRepairReceipt wWMSSubRepairReceipt = GetWMSSubRepairReceipt(wPickDemand);
            wHeaderList.add(wWMSSubRepairReceipt);
            WMSSubRepairReceipts wWMSSubRepairReceipts = new WMSSubRepairReceipts(wHeaderList);
            WMSReturn wReturn = new WMSReturn();

            if (wReturn.returnFlag.equals("1")) {
                wPickDemand.Status = WMSPickDemandStatus.ToJJ.getValue();
                wPickDemand.SendStatus = 1;
                wPickDemand.SendDesc = "推送成功";

                // 关闭入库申请消息
                CoreServiceImpl.getInstance().BFC_MsgHandleTask(wLoginUser,
                        BPMEventModule.SelfRepairInstock.getValue(), 0, 3,
                        new ArrayList<>(Collections.singletonList(wPickDemand.ID)), wLoginUser.ID, 1);

                // 发送待办消息给申请人进行委外拆修入库交接
                List<BFCMessage> wBFCMessageList = new ArrayList<>();
                BFCMessage wMessage = null;
                SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
                int wShiftID = Integer.parseInt(wSDF.format(Calendar.getInstance().getTime()));

                List<Integer> wUserIDList = new ArrayList<>(Collections.singletonList(wPickDemand.CreateID));
                String wMessageText = StringUtils.Format("【{0}】-【{2}】-【{3}】 {1}已发起自修件入库申请，请及时完成入库交接。",
                        BPMEventModule.SelfRepairRKJJ.getLable(), wLoginUser.Name, wPickDemand.PartNo,
                        wPickDemand.PartName);

                for (int wUserID : wUserIDList) {
                    // 发送任务消息到人员
                    wMessage = new BFCMessage();
                    wMessage.Active = 0;
                    wMessage.CompanyID = 0;
                    wMessage.CreateTime = Calendar.getInstance();
                    wMessage.EditTime = Calendar.getInstance();
                    wMessage.ID = 0;
                    wMessage.MessageID = wPickDemand.ID;
                    wMessage.Title = wPickDemand.DemandNo;
                    wMessage.MessageText = wMessageText;
                    wMessage.ModuleID = BPMEventModule.SelfRepairRKJJ.getValue();
                    wMessage.ResponsorID = wUserID;
                    wMessage.ShiftID = wShiftID;
                    wMessage.StationID = 0;
                    wMessage.Type = BFCMessageType.Task.getValue();
                    wBFCMessageList.add(wMessage);
                }

                CoreServiceImpl.getInstance().BFC_UpdateMessageList(BaseDAO.SysAdmin, wBFCMessageList);
            } else {
                wPickDemand.SendStatus = 2;

                if (StringUtils.isEmpty(wReturn.returnDesc))
                    wPickDemand.SendDesc = "网络错误";
                else
                    wPickDemand.SendDesc = wReturn.returnDesc;

                wResult.FaultCode += wReturn.returnDesc;
            }
            WMSPickDemandDAO.getInstance().Update(wLoginUser, wPickDemand, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SelfTaskCodeScanningHandover(BMSEmployee wLoginUser, String wDemandNo) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<WMSPickDemand> wWMSPickDemandList = WMSPickDemandDAO.getInstance().SelectList(wLoginUser, -1, "201",
                    wDemandNo, -1, -1,
                    -1, "", -1, -1, null, null, null, wErrorCode);
            if (wWMSPickDemandList == null || wWMSPickDemandList.size() <= 0) {
                wResult.FaultCode += "提示：二维码错误!";
                return wResult;
            }

            WMSPickDemand wWMSPickDemand = wWMSPickDemandList.get(0);
            wWMSPickDemand.Status = WMSPickDemandStatus.JJed.getValue();
            wWMSPickDemand.DeliveryTime = Calendar.getInstance();
            wWMSPickDemand.ReceiveTime = Calendar.getInstance();
            wWMSPickDemand.SorterTime = Calendar.getInstance();
            wResult.Result = WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);

            // 关闭待办消息
            CoreServiceImpl.getInstance().BFC_MsgHandleTask(wLoginUser,
                    BPMEventModule.SelfRepairInstock.getValue(),
                    0, 3, new ArrayList<>(Collections.singletonList(wWMSPickDemand.ID)), wWMSPickDemand.DeliveryID, 1);
            CoreServiceImpl.getInstance().BFC_MsgHandleTask(wLoginUser,
                    BPMEventModule.SelfRepairRKJJ.getValue(),
                    0, 3, new ArrayList<>(Collections.singletonList(wWMSPickDemand.ID)), wLoginUser.ID, 1);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<OMSOrder>> OMS_QueryPlanMainList(BMSEmployee wLoginUser, int wProductID, int wLineID,
                                                               int wCustomerID, int wOrderID, Calendar wStartTime,
                                                               Calendar wEndTime, int wAPSShiftPeriod) {
        ServiceResult<List<OMSOrder>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①获取计划进厂日期在指定日期范围内计划的订单ID集合
            List<Integer> wOrderIDList = APSTaskPartDAO.getInstance().OMS_QueryOrderIDListByPlanInPlantDate(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderID, wStartTime, wEndTime, wErrorCode);
            // ②查询月计划制定日期在指定日期范围内计划的月计划列表
            List<APSTaskPart> wAPSTaskPartList = APSTaskPartDAO.getInstance().APS_QueryListByTime(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderID, wStartTime, wEndTime, wAPSShiftPeriod, wErrorCode);
            // ③提取所有订单ID集合，查询订单列表
            List<Integer> wIDList = wAPSTaskPartList.stream().map(p -> p.OrderID).distinct()
                    .collect(Collectors.toList());
            for (int wTempID : wIDList) {
                if (wOrderIDList.stream().noneMatch(p -> p == wTempID)) {
                    wOrderIDList.add(wTempID);
                }
            }
            List<OMSOrder> wOMSOrderList = OMSOrderDAO.getInstance().SelectListByIDList(wLoginUser, wOrderIDList,
                    wErrorCode);
            // ④遍历订单，渲染起始工位、结束工位、制定日期、制定人等信息
            for (OMSOrder wOMSOrder : wOMSOrderList) {
                if (wAPSTaskPartList.stream().noneMatch(p -> p.OrderID == wOMSOrder.ID)) {
                    continue;
                }

                List<APSTaskPart> wList = wAPSTaskPartList.stream().filter(p -> p.OrderID == wOMSOrder.ID)
                        .collect(Collectors.toList());
                APSTaskPart wMaxTaskPart = wList.stream().max(Comparator.comparing(APSTaskPart::getStartTime)).get();
                APSTaskPart wMinTaskPart = wList.stream().min(Comparator.comparing(APSTaskPart::getStartTime)).get();
                wOMSOrder.StartPartID = wMinTaskPart.PartID;
                wOMSOrder.StartPartName = wMinTaskPart.PartName;
                wOMSOrder.EndPartID = wMaxTaskPart.PartID;
                wOMSOrder.EndPartName = wMaxTaskPart.PartName;
                wOMSOrder.PlanMakeDate = wList.get(0).SubmitTime;
                wOMSOrder.MakePersonID = wList.get(0).PlanerID;
                wOMSOrder.MakePersonName = wList.get(0).PlanerName;
                wOMSOrder.PlanStartDate = wMinTaskPart.StartTime;
                wOMSOrder.PlanEndDate = wMaxTaskPart.EndTime;
                wOMSOrder.IsHasPlan = 1;
            }
            // ⑤数据返回给前端
            wResult.Result = wOMSOrderList;

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSTaskPart>> APS_QueryMonthPlanByMonthNew(BMSEmployee wLoginUser, int wProductID,
                                                                         int wLineID, int wCustomerID, int wOrderID,
                                                                         int wAreaID, int wPartID, int wAPSShiftPeriod,
                                                                         Calendar wStartTime, Calendar wEndTime) {
        ServiceResult<List<APSTaskPart>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        try {
            wResult.Result = APSTaskPartDAO.getInstance().APS_QueryMonthPlanByMonthNew(wLoginUser, wProductID,
                    wLineID, wCustomerID, wOrderID, wAreaID, wPartID, wAPSShiftPeriod, wStartTime, wEndTime,
                    wErrorCode);

            List<OMSOrder> wOMSOrderList = new ArrayList<>();
            if (wResult.Result.size() > 0) {
                List<Integer> wOrderIDList = wResult.Result.stream().map(p -> p.OrderID).distinct()
                        .collect(Collectors.toList());
                wOMSOrderList = OMSOrderDAO.getInstance().SelectListByIDList(wLoginUser, wOrderIDList, wErrorCode);
            }
            wResult.CustomResult.put("OMSOrderList", wOMSOrderList);

            // 多条件去重
            wResult.Result = wResult.Result.stream()
                    .collect(Collectors.collectingAndThen(Collectors.toCollection(
                                    () -> new TreeSet<>(Comparator.comparing(o -> o.getOrderID() + ";" + o.getPartID()))),
                            ArrayList::new));

            // 工位排序
            OrderPart(wResult.Result);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 工位排序
     */
    private void OrderPart(List<APSTaskPart> wList) {
        try {
            List<LFSWorkAreaStation> wWSList = new ArrayList<>(APSConstans.GetLFSWorkAreaStationList().values());
            for (APSTaskPart wAPSTaskPart : wList) {
                if (wWSList.stream().anyMatch(p -> p.StationID == wAPSTaskPart.PartID)) {
                    wAPSTaskPart.PartOrder = wWSList.stream().filter(p -> p.StationID == wAPSTaskPart.PartID)
                            .findFirst().get().OrderNum;
                } else {
                    wAPSTaskPart.PartOrder = 0;
                }
            }
            // 排序
            wList.sort(Comparator.comparing(APSTaskPart::getPartOrder));
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

    @Override
    public ServiceResult<List<OMSOrder>> OMS_QueryHistoryOrderListNew(BMSEmployee wLoginUser, Calendar wStartTime,
                                                                      Calendar wEndTime, int wAPSShiftPeriod) {
        ServiceResult<List<OMSOrder>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        OutResult<Integer> wErrorCode = new OutResult<>();
        try {
            // 月计划订单
            if (wAPSShiftPeriod == APSShiftPeriod.Month.getValue()) {
                List<Integer> wStateIDList = new ArrayList<>(Arrays.asList(2, 3, 4));

                List<OMSOrder> wList = OMSOrderDAO.getInstance().SelectList(wLoginUser, -1, -1, "", -1, -1, -1, "", "",
                        1, wStateIDList, null, null, null, null, wErrorCode);
                if (wList == null || wList.size() <= 0) {
                    return wResult;
                }
                Calendar wBaseTime = Calendar.getInstance();
                wBaseTime.set(2010, 0, 1);
                wResult.Result = wList.stream().filter(
                                p -> wStateIDList.stream().anyMatch(q -> q == p.Status) && StringUtils.isNotEmpty(p.WBSNo))
                        .collect(Collectors.toList());

            } else if (wAPSShiftPeriod == APSShiftPeriod.Week.getValue()) {// 周计划订单
                List<Integer> wStateList = new ArrayList<>(
                        Arrays.asList(OMSOrderStatus.HasOrder.getValue(), OMSOrderStatus.ReceivedTelegraph.getValue()));
                wResult.Result.addAll(OMSOrderDAO.getInstance().SelectList(wLoginUser, -1, -1, "", -1, -1, -1, "", "",
                        1, wStateList, wStartTime, wEndTime, null, null, wErrorCode));

                wStateList = new ArrayList<>(
                        Arrays.asList(OMSOrderStatus.EnterFactoryed.getValue(), OMSOrderStatus.Repairing.getValue()));
                wResult.Result.addAll(OMSOrderDAO.getInstance().SelectList(wLoginUser, -1, -1, "", -1, -1, -1, "", "",
                        1, wStateList, null, null, null, null, wErrorCode));

                wStateList = new ArrayList<>(
                        Arrays.asList(OMSOrderStatus.FinishedWork.getValue(), OMSOrderStatus.CarSended.getValue()));
                wResult.Result.addAll(OMSOrderDAO.getInstance().SelectList(wLoginUser, -1, -1, "", -1, -1, -1, "", "",
                        1, wStateList, null, null, wStartTime, wEndTime, wErrorCode));
                // ①已收电报、已进场、维修中 可排周计划
                wResult.Result.removeIf(p -> p.Status != OMSOrderStatus.EnterFactoryed.getValue()
                        && p.Status != OMSOrderStatus.Repairing.getValue()
                        && p.Status != OMSOrderStatus.ReceivedTelegraph.getValue());
            }

            // 排序
            if (wResult.Result.size() > 0) {
                wResult.Result.sort(Comparator.comparing(OMSOrder::getStatus, Comparator.reverseOrder())
                        .thenComparing(OMSOrder::getRealReceiveDate));
            }

            // 查询工位排程显示顺序
            List<LFSWorkAreaStation> wLFSWorkAreaStationList = LFSServiceImpl.getInstance()
                    .LFS_QueryWorkAreaStationList(wLoginUser).List(LFSWorkAreaStation.class);

            // 处理计划完工工位
            List<Integer> wOrderIDList = wResult.Result.stream().map(p -> p.ID).distinct().collect(Collectors.toList());
            List<APSTaskPart> apsTaskParts = APSTaskPartDAO.getInstance().SelectListByOrderIDList(wLoginUser,
                    wAPSShiftPeriod, null, wOrderIDList, wErrorCode);
            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);
            for (OMSOrder wOMSOrder : wResult.Result) {
                wOMSOrder.PlanEndDate = wBaseTime;
                wOMSOrder.PlanMakeDate = wBaseTime;
                if (apsTaskParts.stream().noneMatch(p -> p.OrderID == wOMSOrder.ID)) {
                    continue;
                }

                List<APSTaskPart> wList = apsTaskParts.stream().filter(p -> p.OrderID == wOMSOrder.ID)
                        .collect(Collectors.toList());
                APSTaskPart apsTaskPart = wList.stream().max(Comparator.comparing(APSTaskPart::getEndTime)).get();
                wOMSOrder.EndPartID = apsTaskPart.PartID;
                wOMSOrder.EndPartName = apsTaskPart.PartName;
                wOMSOrder.PlanEndDate = apsTaskPart.EndTime;
                wOMSOrder.PlanMakeDate = apsTaskPart.SubmitTime;
                wOMSOrder.PartsNumber = OMSOrderDAO.getInstance().FPC_QueryPartsNumberByRoute(wLoginUser,
                        wOMSOrder.RouteID, wErrorCode);
                wOMSOrder.PartsPlanDoneNumber = (int) apsTaskParts.stream().filter(p -> wLFSWorkAreaStationList.stream()
                        .anyMatch(q -> q.StationID == p.PartID && q.PageNumber == 4 && q.Active == 1)).count();
                wOMSOrder.IsHasPlan = 1;
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSDayPlanMain>> APS_QueryDayPlanMainList(BMSEmployee wLoginUser, Calendar wStartTime,
                                                                        Calendar wEndTime, int wProductID,
                                                                        int wLineID, int wCustomerID, int wOrderID,
                                                                        int wAreaID, int wPartID, int wStatus) {
        ServiceResult<List<APSDayPlanMain>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().APS_QueryDayPlanMainList(wLoginUser,
                    wStartTime,
                    wEndTime, wProductID,
                    wLineID, wCustomerID, wOrderID,
                    wAreaID, wPartID, wStatus, wErrorCode);

            // 多条件
            List<APSTaskStep> wList = wAPSTaskStepList.stream()
                    .collect(Collectors.collectingAndThen(Collectors.toCollection(
                                    () -> new TreeSet<>(Comparator.comparing(o -> o.getOrderID() + ";" + o.getPartID()))),
                            ArrayList::new));
            // 遍历获取日计划
            for (APSTaskStep wAPSTaskStep : wList) {
                List<APSTaskStep> wTaskList = wAPSTaskStepList.stream()
                        .filter(p -> p.OrderID == wAPSTaskStep.OrderID && p.PartID == wAPSTaskStep.PartID)
                        .collect(Collectors.toList());
                int partPointTotalQuantity = wTaskList.size();
                int toScheduledQuantity = (int) wTaskList.stream()
                        .filter(p -> p.Status == APSTaskStatus.Saved.getValue()).count();
                int finishQuantity = (int) wTaskList.stream().filter(p -> p.Status == APSTaskStatus.Done.getValue())
                        .count();
                int doneScheduledQuantity = partPointTotalQuantity - toScheduledQuantity;

                APSDayPlanMain wAPSDayPlanMain = new APSDayPlanMain(wAPSTaskStep.ProductID, wAPSTaskStep.ProductNo,
                        wAPSTaskStep.LineID, wAPSTaskStep.LineName,
                        wAPSTaskStep.CustomerID, wAPSTaskStep.CustomerName, wAPSTaskStep.OrderID, wAPSTaskStep.PartNo,
                        wAPSTaskStep.AreaID,
                        wAPSTaskStep.AreaName, wAPSTaskStep.PartID, wAPSTaskStep.PartName, wAPSTaskStep.CreateTime,
                        wAPSTaskStep.StartTime, partPointTotalQuantity, toScheduledQuantity, finishQuantity,
                        doneScheduledQuantity);
                wResult.Result.add(wAPSDayPlanMain);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSTaskStep>> APS_QueryDayPlanDetails(BMSEmployee wLoginUser, int wOrderID, int wPartID) {
        ServiceResult<List<APSTaskStep>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1, -1, -1, -1, wPartID,
                    -1, -1, 1,
                    null, null, null, null, wErrorCode);

            if (wResult.Result.size() <= 0) {
                return wResult;
            }

            List<Integer> wTaskStepIDList = wResult.Result.stream().map(p -> p.ID).distinct()
                    .collect(Collectors.toList());
            List<Integer> wStepIDList = wResult.Result.stream().map(p -> p.StepID).distinct()
                    .collect(Collectors.toList());

            // ①获取工位作业班组基础数据
            List<BMSWorkCharge> wBMSWorkChargeList = CoreServiceImpl.getInstance().FMC_QueryWorkChargeList(wLoginUser)
                    .List(BMSWorkCharge.class);
            // ②获取派工数据
            List<SFCTaskStep> wSFCTaskStepList = SFCTaskStepDAO.getInstance().SelectListByTaskStepIDList(wLoginUser,
                    wTaskStepIDList, -1, wErrorCode);
            // ③获取检验单数据
            List<SFCTaskIPT> wSFCTaskIPTList = QMSServiceImpl.getInstance()
                    .SFC_QueryTaskIPTListByTaskStepIDList(wLoginUser, wTaskStepIDList).List(SFCTaskIPT.class);
            // ④获取部门列表
            List<BMSDepartment> wBMSDepartmentList = CoreServiceImpl.getInstance()
                    .BMS_QueryDepartmentList(wLoginUser, -1, -1).List(BMSDepartment.class);
            // ⑤获取岗位列表
            List<BMSPosition> wBMSPositionList = CoreServiceImpl.getInstance().BMS_QueryPositionList(wLoginUser, -1)
                    .List(BMSPosition.class);

            // 获取不合格评审标记
            boolean wIsHasNCR = APSTaskStepDAO.getInstance().JudgeIsHasNCR(wLoginUser, wOrderID, wPartID, wErrorCode);
            // 物料领用单数据
            Map<Integer, Integer> wPickDemandMap = APSTaskStepDAO.getInstance().WMS_QueryPickDemandMap(wLoginUser,
                    wOrderID, wPartID, wStepIDList, wErrorCode);
            // 偶换件评审单数据
            Map<Integer, Integer> wSFCBOMTaskMap = APSTaskStepDAO.getInstance().WMS_QueryBOMTaskMap(wLoginUser,
                    wOrderID, wPartID, wStepIDList, wErrorCode);

            // 遍历赋值
            for (APSTaskStep wAPSTaskStep : wResult.Result) {
                // 不合格评审标记
                if (wIsHasNCR) {
                    wAPSTaskStep.NCRFlag = 1;
                }
                // 三检标记
                if (wSFCTaskIPTList.stream().anyMatch(p -> p.TaskStepID == wAPSTaskStep.ID)) {
                    wAPSTaskStep.CheckFlag = 1;
                }
                // 物料领用单标记
                if (wPickDemandMap.containsKey(wAPSTaskStep.StepID)) {
                    wAPSTaskStep.PickDemandFlag = wPickDemandMap.get(wAPSTaskStep.StepID);
                }
                // 偶换件评审单标记
                if (wSFCBOMTaskMap.containsKey(wAPSTaskStep.StepID)) {
                    wAPSTaskStep.BOMTaskFlag = wSFCBOMTaskMap.get(wAPSTaskStep.StepID);
                }

                List<SFCTaskIPT> wIPTList = wSFCTaskIPTList.stream().filter(p -> p.TaskStepID == wAPSTaskStep.ID)
                        .collect(Collectors.toList());
                if (wAPSTaskStep.Status == 1) {
                    wAPSTaskStep.StatusText = "未提交";
                } else if (wAPSTaskStep.Status == 2) {
                    wAPSTaskStep.StatusText = "待开工";
                } else if (wAPSTaskStep.Status == 4) {
                    wAPSTaskStep.StatusText = "已开工";
                } else if (wAPSTaskStep.Status == 5) {
                    wAPSTaskStep.StatusText = "已完工";
                }
                // 状态
                if (wAPSTaskStep.Status == APSTaskStatus.Started.getValue()) {
                    if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.SelfCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待自检";
                    } else if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.MutualCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待互检";
                    } else if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.SpecialCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待专检";
                    } else if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.PreCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待预检";
                    }
                }
                // 责任班组
                if (wAPSTaskStep.IsDispatched
                        && wSFCTaskStepList.stream().anyMatch(p -> p.TaskStepID == wAPSTaskStep.ID)) {
                    List<SFCTaskStep> wDisList = wSFCTaskStepList.stream().filter(p -> p.TaskStepID == wAPSTaskStep.ID)
                            .collect(Collectors.toList());
                    int wMonitorID = wDisList.get(0).MonitorID;
                    BMSEmployee wMonitor = APSConstans.GetBMSEmployee(wMonitorID);
                    if (wBMSDepartmentList.stream().anyMatch(p -> p.ID == wMonitor.DepartmentID)) {
                        wAPSTaskStep.DutyDepartment = wBMSDepartmentList.stream()
                                .filter(p -> p.ID == wMonitor.DepartmentID).findFirst().get().Name;
                        wAPSTaskStep.Monitors = wMonitor.Name;
                    }
                    // 责任人
                    wAPSTaskStep.DutyPersons = StringUtils.Join(",",
                            wDisList.stream().map(p -> p.Operator).collect(Collectors.toList()));
                    // 专检员
                    if (wBMSWorkChargeList.stream()
                            .anyMatch(p -> p.ClassID == wMonitor.DepartmentID && p.StationID == wAPSTaskStep.PartID)) {
                        BMSWorkCharge wTemp = wBMSWorkChargeList.stream()
                                .filter(p -> p.ClassID == wMonitor.DepartmentID && p.StationID == wAPSTaskStep.PartID)
                                .findFirst().get();
                        wAPSTaskStep.CheckerNames = APSUtils.getInstance().GetNames(wTemp.CheckerList);
                    }
                } else {
                    List<BMSWorkCharge> wChargeList = wBMSWorkChargeList.stream()
                            .filter(p -> p.StationID == wAPSTaskStep.PartID && p.Active == 1)
                            .collect(Collectors.toList());
                    if (wChargeList.size() > 0) {
                        wAPSTaskStep.DutyDepartment = StringUtils.Join(",",
                                wChargeList.stream().map(p -> p.ClassName).collect(Collectors.toList()));

                        List<BMSEmployee> wMonitorList = APSConstans.GetBMSEmployeeList().values().stream()
                                .filter(p -> wChargeList.stream().anyMatch(q -> q.ClassID == p.DepartmentID)
                                        && wBMSPositionList.stream().anyMatch(q -> q.ID == p.Position && q.DutyID == 1)
                                        && p.Active == 1)
                                .collect(Collectors.toList());
                        if (wMonitorList.size() > 0) {
                            wAPSTaskStep.Monitors = StringUtils.Join(",",
                                    wMonitorList.stream().map(p -> p.Name).distinct().collect(Collectors.toList()));
                        }
                    }
                }
                // 工艺师
                wAPSTaskStep.CraftMans = APSConstans.GetFPCPart(wAPSTaskStep.PartID).TechnicianName;
                // 专检员
                if (StringUtils.isEmpty(wAPSTaskStep.CheckerNames)) {
                    wAPSTaskStep.CheckerNames = APSConstans.GetFPCPart(wAPSTaskStep.PartID).CheckerName;
                }

                if (wAPSTaskStep.Status == 2 && StringUtils.isEmpty(wAPSTaskStep.DutyPersons)) {
                    wAPSTaskStep.StatusText = "待派工";
                }
            }

            // 查询工序转序详情
            List<RSMTurnOrderDesc> wRSMTurnOrderDescList = RSMTurnOrderDescDAO.getInstance().SelectList(wLoginUser, -1,
                    wOrderID, wPartID, -1, -1, wErrorCode);
            wResult.CustomResult.put("RSMTurnOrderDescList", wRSMTurnOrderDescList);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("APS_QueryDayPlanDetails", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<WMSPickDemandItem>> WMS_QueryPickDemandList(BMSEmployee wLoginUser, int wOrderID,
                                                                          int wPartID, int wPartPointID) {
        ServiceResult<List<WMSPickDemandItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wIDList = WMSPickDemandDAO.getInstance().WMS_QueryListByPartPoint(wLoginUser, wOrderID,
                    wPartID, wPartPointID, wErrorCode);
            if (wIDList != null && wIDList.size() > 0) {
                List<WMSPickDemand> wmsPickDemands = WMSPickDemandDAO.getInstance().SelectList(wLoginUser, -1, "", "",
                        -1, -1,
                        -1, "", -1, -1, null, null, wIDList, wErrorCode);

                wResult.Result = WMSPickDemandItemDAO.getInstance().SelectListByDemandIDList(wLoginUser, wIDList,
                        wErrorCode);

                for (WMSPickDemandItem wmsPickDemandItem : wResult.Result) {
                    if (wmsPickDemands.stream().anyMatch(p -> p.ID == wmsPickDemandItem.DemandID)) {
                        wmsPickDemandItem.WMSPickDemand = wmsPickDemands.stream()
                                .filter(p -> p.ID == wmsPickDemandItem.DemandID).findFirst().get();
                    }
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<SFCBOMTaskItem>> SFC_QuerySFCBOMTaskList(BMSEmployee wLoginUser, int wOrderID,
                                                                       int wPartID, int wPartPointID) {
        ServiceResult<List<SFCBOMTaskItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<SFCBOMTask> sfcbomTasks = SFCBOMTaskDAO.getInstance().SelectList(wLoginUser, wOrderID, wPartID,
                    wPartPointID, -1, -1, null, null, null, null, wErrorCode);
            if (sfcbomTasks != null && sfcbomTasks.size() > 0) {
                List<Integer> wIDList = sfcbomTasks.stream().map(p -> p.ID).distinct().collect(Collectors.toList());
                wResult.Result = SFCBOMTaskItemDAO.getInstance().SelectList(wLoginUser, wIDList, wErrorCode);
                for (SFCBOMTaskItem sfcbomTaskItem : wResult.Result) {
                    if (sfcbomTasks.stream().anyMatch(p -> p.ID == sfcbomTaskItem.SFCBOMTaskID)) {
                        sfcbomTaskItem.SFCBOMTask = sfcbomTasks.stream()
                                .filter(p -> p.ID == sfcbomTaskItem.SFCBOMTaskID).findFirst().get();
                    }
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<OMSCheckMsg>> OMS_Check_V2(BMSEmployee wLoginUser, String wOrderIDs) {
        ServiceResult<List<OMSCheckMsg>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        try {
            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);

            List<BMSWorkCharge> wBMSWorkChargeList = CoreServiceImpl.getInstance().FMC_QueryWorkChargeList(wLoginUser)
                    .List(BMSWorkCharge.class);
            wBMSWorkChargeList = wBMSWorkChargeList.stream().filter(p -> p.Active == 1).collect(Collectors.toList());

            List<OMSOrder> wOMSOrderList = OMSOrderDAO.getInstance().SelectListByIDList(wLoginUser,
                    StringUtils.parseIntList(wOrderIDs.split(",")), wErrorCode);

            List<APSTaskPart> wAPSTaskPartList = APSTaskPartDAO.getInstance().SelectListByOrderIDList(wLoginUser, -1,
                    null,
                    wOMSOrderList.stream().map(p -> p.ID).distinct().collect(Collectors.toList()), wErrorCode);

            for (OMSOrder wOrder : wOMSOrderList) {
                // ①工艺bop
                if (wOrder.RouteID <= 0) {
                    OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.BOP.getValue(),
                            StringUtils.Format("未设置工艺BOP!", wOrder.PartNo), OMSCheckType.BOP.getLable(),
                            wOrder.ProductNo, wOrder.LineName, "", "");
                    wOMSCheckMsg.PartNo = wOrder.PartNo;
                    wOMSCheckMsg.CreateID = wLoginUser.ID;
                    wOMSCheckMsg.CreateTime = Calendar.getInstance();
                    wOMSCheckMsg.EditID = 0;
                    wOMSCheckMsg.EditTime = wBaseTime;
                    wOMSCheckMsg.DutyPerson = "技术中心";
                    wOMSCheckMsg.CloseTime = wBaseTime;
                    wResult.Result.add(wOMSCheckMsg);
                }
                // ②标准bom
                boolean wCheckResult = OMSOrderDAO.getInstance().IsSettedCurrentStandardBOM(wLoginUser, wOrder,
                        wErrorCode);
                if (!wCheckResult) {
                    OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.BOM.getValue(),
                            StringUtils.Format("提示：【{0}】该车辆未设置当前标准BOM!", wOrder.PartNo), OMSCheckType.BOM.getLable(),
                            wOrder.ProductNo, wOrder.LineName, "", "");
                    wOMSCheckMsg.PartNo = wOrder.PartNo;
                    wOMSCheckMsg.CreateID = wLoginUser.ID;
                    wOMSCheckMsg.CreateTime = Calendar.getInstance();
                    wOMSCheckMsg.EditID = 0;
                    wOMSCheckMsg.EditTime = wBaseTime;
                    wOMSCheckMsg.DutyPerson = "技术中心";
                    wOMSCheckMsg.CloseTime = wBaseTime;
                    wResult.Result.add(wOMSCheckMsg);
                }
                // ③检验模板-未设置检验模板
                List<FPCRoutePartPoint> wList = OMSOrderDAO.getInstance().SelectNoSettedStandardList(wLoginUser, wOrder,
                        wErrorCode);
                for (FPCRoutePartPoint wFPCRoutePartPoint : wList) {
                    OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.Standard.getValue(),
                            "无当前过程检验标准!",
                            OMSCheckType.Standard.getLable());
                    wOMSCheckMsg.ProductNo = wOrder.ProductNo;
                    wOMSCheckMsg.LineName = wOrder.LineName;
                    wOMSCheckMsg.PartName = wFPCRoutePartPoint.PartName;
                    wOMSCheckMsg.PartPointName = wFPCRoutePartPoint.PartPointName;
                    wOMSCheckMsg.PartNo = wOrder.PartNo;
                    wOMSCheckMsg.CreateID = wLoginUser.ID;
                    wOMSCheckMsg.CreateTime = Calendar.getInstance();
                    wOMSCheckMsg.EditID = 0;
                    wOMSCheckMsg.EditTime = wBaseTime;
                    wOMSCheckMsg.DutyPerson = APSUtils.getInstance()
                            .GetNames(APSConstans.GetFPCPart(wFPCRoutePartPoint.PartID).TechnicianList);
                    if (wAPSTaskPartList.stream()
                            .anyMatch(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePartPoint.PartID)) {
                        wOMSCheckMsg.CloseTime = wAPSTaskPartList.stream()
                                .filter(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePartPoint.PartID)
                                .findFirst().get().StartTime;
                    } else {
                        wOMSCheckMsg.CloseTime = wBaseTime;
                    }
                    if (wResult.Result.stream().noneMatch(p -> p.ProductNo.equals(wOMSCheckMsg.ProductNo)
                            && p.LineName.equals(wOMSCheckMsg.LineName)
                            && p.PartName.equals(wOMSCheckMsg.PartName)
                            && p.PartPointName.equals(wOMSCheckMsg.PartPointName)
                            && p.Message.equals(wOMSCheckMsg.Message))) {
                        wResult.Result.add(wOMSCheckMsg);
                    }
                }
                // ④检验模板-导入模板
                List<FPCRoutePartPoint> wImportList = OMSOrderDAO.getInstance().SelectImportDataList(wLoginUser, wOrder,
                        wErrorCode);
                for (FPCRoutePartPoint wFPCRoutePartPoint : wImportList) {
                    OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.Standard.getValue(),
                            "当前过程检验标准为导入模板，请及时修改检验内容!",
                            OMSCheckType.Standard.getLable());
                    wOMSCheckMsg.ProductNo = wOrder.ProductNo;
                    wOMSCheckMsg.LineName = wOrder.LineName;
                    wOMSCheckMsg.PartName = wFPCRoutePartPoint.PartName;
                    wOMSCheckMsg.PartPointName = wFPCRoutePartPoint.PartPointName;
                    wOMSCheckMsg.PartNo = wOrder.PartNo;
                    wOMSCheckMsg.CreateID = wLoginUser.ID;
                    wOMSCheckMsg.CreateTime = Calendar.getInstance();
                    wOMSCheckMsg.EditID = 0;
                    wOMSCheckMsg.EditTime = wBaseTime;
                    wOMSCheckMsg.DutyPerson = APSUtils.getInstance()
                            .GetNames(APSConstans.GetFPCPart(wFPCRoutePartPoint.PartID).TechnicianList);
                    if (wAPSTaskPartList.stream()
                            .anyMatch(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePartPoint.PartID)) {
                        wOMSCheckMsg.CloseTime = wAPSTaskPartList.stream()
                                .filter(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePartPoint.PartID)
                                .findFirst().get().StartTime;
                    } else {
                        wOMSCheckMsg.CloseTime = wBaseTime;
                    }
                    if (wResult.Result.stream().noneMatch(p -> p.ProductNo.equals(wOMSCheckMsg.ProductNo)
                            && p.LineName.equals(wOMSCheckMsg.LineName)
                            && p.PartName.equals(wOMSCheckMsg.PartName)
                            && p.PartPointName.equals(wOMSCheckMsg.PartPointName)
                            && p.Message.equals(wOMSCheckMsg.Message))) {
                        wResult.Result.add(wOMSCheckMsg);
                    }
                }
                // ⑤工位作业班组-未配置作业班组
                List<FPCRoutePart> wFPCRoutePartList = FMCServiceImpl.getInstance()
                        .FPC_QueryRoutePartListByRouteID(wLoginUser,
                                wOrder.RouteID)
                        .List(FPCRoutePart.class);
                for (FPCRoutePart wFPCRoutePart : wFPCRoutePartList) {
                    if (wBMSWorkChargeList.stream().noneMatch(p -> p.StationID == wFPCRoutePart.PartID)) {
                        OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.WorkCharge.getValue(),
                                "该工位未配置作业班组!",
                                OMSCheckType.WorkCharge.getLable());
                        wOMSCheckMsg.ProductNo = wOrder.ProductNo;
                        wOMSCheckMsg.LineName = wOrder.LineName;
                        wOMSCheckMsg.PartName = APSConstans.GetFPCPartName(wFPCRoutePart.PartID);
                        wOMSCheckMsg.PartPointName = "";
                        wOMSCheckMsg.PartNo = wOrder.PartNo;
                        wOMSCheckMsg.CreateID = wLoginUser.ID;
                        wOMSCheckMsg.CreateTime = Calendar.getInstance();
                        wOMSCheckMsg.EditID = 0;
                        wOMSCheckMsg.EditTime = wBaseTime;
                        wOMSCheckMsg.DutyPerson = "生产/工艺";
                        if (wAPSTaskPartList.stream()
                                .anyMatch(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePart.PartID)) {
                            wOMSCheckMsg.CloseTime = wAPSTaskPartList.stream()
                                    .filter(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePart.PartID).findFirst()
                                    .get().StartTime;
                        } else {
                            wOMSCheckMsg.CloseTime = wBaseTime;
                        }
                        if (wResult.Result.stream().noneMatch(p -> p.ProductNo.equals(wOMSCheckMsg.ProductNo)
                                && p.LineName.equals(wOMSCheckMsg.LineName)
                                && p.PartName.equals(wOMSCheckMsg.PartName)
                                && p.PartPointName.equals(wOMSCheckMsg.PartPointName)
                                && p.Message.equals(wOMSCheckMsg.Message))) {
                            wResult.Result.add(wOMSCheckMsg);
                        }

                        continue;
                    }

                    List<BMSWorkCharge> wChargeList = wBMSWorkChargeList.stream()
                            .filter(p -> p.StationID == wFPCRoutePart.PartID).collect(Collectors.toList());
                    for (BMSWorkCharge wBMSWorkCharge : wChargeList) {
                        // ⑥工位作业班组-未配置工艺师
                        if (wBMSWorkCharge.TechnicianList == null || wBMSWorkCharge.TechnicianList.size() <= 0) {
                            OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.WorkCharge.getValue(),
                                    StringUtils.Format("【{0}】工位作业班组未配置工艺师!",
                                            APSConstans.GetBMSDepartmentName(wBMSWorkCharge.ClassID)),
                                    OMSCheckType.WorkCharge.getLable());

                            wOMSCheckMsg.ProductNo = wOrder.ProductNo;
                            wOMSCheckMsg.LineName = wOrder.LineName;
                            wOMSCheckMsg.PartName = APSConstans.GetFPCPartName(wFPCRoutePart.PartID);
                            wOMSCheckMsg.PartPointName = "";
                            wOMSCheckMsg.PartNo = wOrder.PartNo;
                            wOMSCheckMsg.CreateID = wLoginUser.ID;
                            wOMSCheckMsg.CreateTime = Calendar.getInstance();
                            wOMSCheckMsg.EditID = 0;
                            wOMSCheckMsg.EditTime = wBaseTime;
                            wOMSCheckMsg.DutyPerson = "技术质量室";
                            if (wAPSTaskPartList.stream()
                                    .anyMatch(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePart.PartID)) {
                                wOMSCheckMsg.CloseTime = wAPSTaskPartList.stream()
                                        .filter(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePart.PartID)
                                        .findFirst().get().StartTime;
                            } else {
                                wOMSCheckMsg.CloseTime = wBaseTime;
                            }

                            if (wResult.Result.stream().noneMatch(p -> p.ProductNo.equals(wOMSCheckMsg.ProductNo)
                                    && p.LineName.equals(wOMSCheckMsg.LineName)
                                    && p.PartName.equals(wOMSCheckMsg.PartName)
                                    && p.PartPointName.equals(wOMSCheckMsg.PartPointName)
                                    && p.Message.equals(wOMSCheckMsg.Message))) {
                                wResult.Result.add(wOMSCheckMsg);
                            }

                        }
                        // ⑦工位作业班组-未配置检查员
                        if (wBMSWorkCharge.CheckerList == null || wBMSWorkCharge.CheckerList.size() <= 0) {
                            OMSCheckMsg wOMSCheckMsg = new OMSCheckMsg(OMSCheckType.WorkCharge.getValue(),
                                    StringUtils.Format("【{0}】该工位作业班组未配置检查员!",
                                            APSConstans.GetBMSDepartmentName(wBMSWorkCharge.ClassID)),
                                    OMSCheckType.WorkCharge.getLable());

                            wOMSCheckMsg.ProductNo = wOrder.ProductNo;
                            wOMSCheckMsg.LineName = wOrder.LineName;
                            wOMSCheckMsg.PartName = APSConstans.GetFPCPartName(wFPCRoutePart.PartID);
                            wOMSCheckMsg.PartPointName = "";
                            wOMSCheckMsg.PartNo = wOrder.PartNo;
                            wOMSCheckMsg.CreateID = wLoginUser.ID;
                            wOMSCheckMsg.CreateTime = Calendar.getInstance();
                            wOMSCheckMsg.EditID = 0;
                            wOMSCheckMsg.EditTime = wBaseTime;
                            wOMSCheckMsg.DutyPerson = "质量管理部";
                            if (wAPSTaskPartList.stream()
                                    .anyMatch(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePart.PartID)) {
                                wOMSCheckMsg.CloseTime = wAPSTaskPartList.stream()
                                        .filter(p -> p.OrderID == wOrder.ID && p.PartID == wFPCRoutePart.PartID)
                                        .findFirst().get().StartTime;
                            } else {
                                wOMSCheckMsg.CloseTime = wBaseTime;
                            }
                            if (wResult.Result.stream().noneMatch(p -> p.ProductNo.equals(wOMSCheckMsg.ProductNo)
                                    && p.LineName.equals(wOMSCheckMsg.LineName)
                                    && p.PartName.equals(wOMSCheckMsg.PartName)
                                    && p.PartPointName.equals(wOMSCheckMsg.PartPointName)
                                    && p.Message.equals(wOMSCheckMsg.Message))) {
                                wResult.Result.add(wOMSCheckMsg);
                            }
                        }
                    }
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<OMSCheckMsg>> OMS_QueryCheckMsgList(BMSEmployee wLoginUser, int wType, int wPlanTaskID) {
        ServiceResult<List<OMSCheckMsg>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = OMSCheckMsgDAO.getInstance().SelectList(wLoginUser, -1, wType, wPlanTaskID, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> OMS_UpdateCheckMsg(BMSEmployee wLoginUser, OMSCheckMsg wOMSCheckMsg) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = OMSCheckMsgDAO.getInstance().Update(wLoginUser, wOMSCheckMsg, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> OMS_UpdateCheckMsgList(BMSEmployee wLoginUser, List<OMSCheckMsg> wOMSCheckMsgList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            for (OMSCheckMsg wOMSCheckMsg : wOMSCheckMsgList) {
                wResult.Result = OMSCheckMsgDAO.getInstance().Update(wLoginUser, wOMSCheckMsg, wErrorCode);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<FPCRoutePart>> FPC_QueryStationDetails(BMSEmployee wLoginUser, int wOrderID,
                                                                     int wAPSShiftPeriod) {
        ServiceResult<List<FPCRoutePart>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            OMSOrder wOMSOrder = OMSOrderDAO.getInstance().SelectByID(wLoginUser, wOrderID, wErrorCode);
            if (wOMSOrder.RouteID <= 0) {
                return wResult;
            }

            wResult.Result = FMCServiceImpl.getInstance().FPC_QueryRoutePartListByRouteID(wLoginUser,
                    wOMSOrder.RouteID).List(FPCRoutePart.class);
            wResult.Result.sort(Comparator.comparing(FPCRoutePart::getOrderID));

            // 查询工位计划
            List<APSTaskPart> wAPSTaskPartList = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, -1, 1, wAPSShiftPeriod, null, -1, null, null, wErrorCode);
            List<APSTaskPart> wWeekTaskPartList = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, -1, 1, 5, null, -1, null, null, wErrorCode);
            // 查询工序计划
            List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, -1, -1, -1, -1, -1, 1, null, null, null, null, wErrorCode);

            wResult.CustomResult.put("OMSOrder", wOMSOrder);

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);
            for (FPCRoutePart wFPCRoutePart : wResult.Result) {
                // ①计划日期
                if (wAPSTaskPartList.stream().anyMatch(p -> p.PartID == wFPCRoutePart.PartID)) {
                    wFPCRoutePart.PlanStartDate = wAPSTaskPartList.stream()
                            .filter(p -> p.PartID == wFPCRoutePart.PartID).findFirst().get().StartTime;
                    wFPCRoutePart.PlanEndDate = wAPSTaskPartList.stream().filter(p -> p.PartID == wFPCRoutePart.PartID)
                            .findFirst().get().EndTime;
                } else {
                    wFPCRoutePart.PlanStartDate = wBaseTime;
                    wFPCRoutePart.PlanEndDate = wBaseTime;
                }
                // ②完工日期
                if (wWeekTaskPartList.stream().anyMatch(p -> p.PartID == wFPCRoutePart.PartID)) {
                    wFPCRoutePart.FinishDate = wWeekTaskPartList.stream().filter(p -> p.PartID == wFPCRoutePart.PartID)
                            .findFirst().get().FinishWorkTime;
                } else {
                    wFPCRoutePart.FinishDate = wBaseTime;
                }
                // ③工序完工数量
                if (wAPSTaskStepList.stream().anyMatch(p -> p.PartID == wFPCRoutePart.PartID)) {
                    wFPCRoutePart.PartPointFinishSize = (int) wAPSTaskStepList.stream()
                            .filter(p -> p.PartID == wFPCRoutePart.PartID && p.Status == 5).count();
                } else {
                    wFPCRoutePart.PartPointFinishSize = 0;
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<RSMTurnOrderDesc>> RSM_QueryTurnOrderDescList(BMSEmployee wLoginUser, int wOrderID,
                                                                            int wPartID, int wPartPointID,
                                                                            int wIsMaterialShortage) {
        ServiceResult<List<RSMTurnOrderDesc>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = RSMTurnOrderDescDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, wPartID,
                    wPartPointID, wIsMaterialShortage, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> RSM_UpdateTurnOrderDesc(BMSEmployee wLoginUser, RSMTurnOrderDesc wRSMTurnOrderDesc) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wRSMTurnOrderDesc.ID <= 0) {
                List<RSMTurnOrderDesc> wRSMTurnOrderDescList = RSMTurnOrderDescDAO.getInstance().SelectList(wLoginUser,
                        -1, wRSMTurnOrderDesc.OrderID, wRSMTurnOrderDesc.PartID, wRSMTurnOrderDesc.PartPointID, -1,
                        wErrorCode);
                if (wRSMTurnOrderDescList.size() > 0) {
                    wResult.FaultCode += "提示：该工序的转序信息已录入，请修改!";
                    return wResult;
                }

                wRSMTurnOrderDesc.CreateID = wLoginUser.ID;
                wRSMTurnOrderDesc.CreateTime = Calendar.getInstance();
            }

            wRSMTurnOrderDesc.EditID = wLoginUser.ID;
            wRSMTurnOrderDesc.EditTime = Calendar.getInstance();

            wResult.Result = RSMTurnOrderDescDAO.getInstance().Update(wLoginUser, wRSMTurnOrderDesc, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<WMSPickDemandItem>> WMS_QueryPickDemandItemList(BMSEmployee wLoginUser, int wOrderID,
                                                                              int wPartID, int wPartPointID,
                                                                              String wMaterial) {
        ServiceResult<List<WMSPickDemandItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = WMSPickDemandItemDAO.getInstance().WMS_QueryPickDemandItemList(wLoginUser, wOrderID,
                    wPartID, wPartPointID, wMaterial, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public void WMS_CancelPickDemandListByFinishedOrder(BMSEmployee wLoginUser, OMSOrder wOrder,
                                                        OutResult<Integer> wErrorCode) {
        try {
            boolean wIsActiveRowHandOver = Configuration.readConfigString("IsActiveRowHandOver", "config/config")
                    .equals("1");
            if (wIsActiveRowHandOver) {
                List<WMSPickDemand> wWMSPickDemandList = WMSPickDemandDAO.getInstance().SelectList(wLoginUser, -1, "",
                        "",
                        -1, -1,
                        -1, String.valueOf(wOrder.ID), -1, -1, null, null, null,
                        wErrorCode);
                wWMSPickDemandList.removeIf(p -> p.Status == WMSPickDemandStatusNew.FinishClosed.getValue()
                        || p.Status == WMSPickDemandStatusNew.Finished.getValue()
                        || p.Status == WMSPickDemandStatusNew.Canceld.getValue());
                if (wWMSPickDemandList.size() <= 0) {
                    return;
                }

                for (WMSPickDemand wWMSPickDemand : wWMSPickDemandList) {
                    // 完工关闭
                    WMSPickDemandDAO.getInstance().CompletedClose(wLoginUser, wWMSPickDemand, wErrorCode);
                }
            } else {
                List<WMSPickDemand> wWMSPickDemandList = WMSPickDemandDAO.getInstance().SelectList(wLoginUser, -1, "",
                        "",
                        -1, -1,
                        -1, String.valueOf(wOrder.ID), -1, WMSPickDemandStatus.Sended.getValue(), null, null, null,
                        wErrorCode);
                if (wWMSPickDemandList.size() <= 0) {
                    return;
                }

                for (WMSPickDemand wWMSPickDemand : wWMSPickDemandList) {
                    wWMSPickDemand.IsFinish = 1;
                    wWMSPickDemand.Remark = "完工车取消";
                    WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                    WMSServiceImpl.getInstance().WMS_CancelDistribution(wLoginUser, wWMSPickDemand.ID);
                }
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

    @Override
    public ServiceResult<Integer> WMS_AgreeReSendPickDemandList(BMSEmployee wLoginUser,
                                                                List<WMSPickDemand> wWMSPickDemandList, int wIsAgree) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            for (WMSPickDemand wWMSPickDemand : wWMSPickDemandList) {
                wWMSPickDemand.IsFinanceAgree = wIsAgree;
                WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSTaskStep>> QueryDayPlanDetailsByOrder(BMSEmployee wLoginUser, int wOrderID) {
        ServiceResult<List<APSTaskStep>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<APSTaskPart> wAPSTaskPartList = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, -1, 1,
                    APSShiftPeriod.Week.getValue(), new ArrayList<>(Collections.singletonList(4)), -1, null, null,
                    wErrorCode);

            // ①获取工位作业班组基础数据
            List<BMSWorkCharge> wBMSWorkChargeList = CoreServiceImpl.getInstance().FMC_QueryWorkChargeList(wLoginUser)
                    .List(BMSWorkCharge.class);
            // ④获取部门列表
            List<BMSDepartment> wBMSDepartmentList = CoreServiceImpl.getInstance()
                    .BMS_QueryDepartmentList(wLoginUser, -1, -1).List(BMSDepartment.class);
            // ⑤获取岗位列表
            List<BMSPosition> wBMSPositionList = CoreServiceImpl.getInstance().BMS_QueryPositionList(wLoginUser, -1)
                    .List(BMSPosition.class);

            for (APSTaskPart wAPSTaskPart : wAPSTaskPartList) {
                List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, wOrderID,
                        -1, -1, -1, -1, wAPSTaskPart.PartID, -1, -1, 1,
                        null, null, null, null, wErrorCode);
                if (wAPSTaskStepList.size() <= 0) {
                    continue;
                }

                List<Integer> wTaskStepIDList = wAPSTaskStepList.stream().map(p -> p.ID).distinct()
                        .collect(Collectors.toList());
                List<Integer> wStepIDList = wAPSTaskStepList.stream().map(p -> p.StepID).distinct()
                        .collect(Collectors.toList());

                // ②获取派工数据
                List<SFCTaskStep> wSFCTaskStepList = SFCTaskStepDAO.getInstance().SelectListByTaskStepIDList(wLoginUser,
                        wTaskStepIDList, -1, wErrorCode);
                // ③获取检验单数据
                List<SFCTaskIPT> wSFCTaskIPTList = QMSServiceImpl.getInstance()
                        .SFC_QueryTaskIPTListByTaskStepIDList(wLoginUser,
                                wTaskStepIDList)
                        .List(SFCTaskIPT.class);

                // 获取不合格评审标记
                boolean wIsHasNCR = APSTaskStepDAO.getInstance().JudgeIsHasNCR(wLoginUser, wOrderID,
                        wAPSTaskPart.PartID,
                        wErrorCode);
                // 物料领用单数据
                Map<Integer, Integer> wPickDemandMap = APSTaskStepDAO.getInstance().WMS_QueryPickDemandMap(wLoginUser,
                        wOrderID, wAPSTaskPart.PartID, wStepIDList, wErrorCode);
                // 偶换件评审单数据
                Map<Integer, Integer> wSFCBOMTaskMap = APSTaskStepDAO.getInstance().WMS_QueryBOMTaskMap(wLoginUser,
                        wOrderID, wAPSTaskPart.PartID, wStepIDList, wErrorCode);

                // 遍历赋值
                for (APSTaskStep wAPSTaskStep : wAPSTaskStepList) {
                    // 不合格评审标记
                    if (wIsHasNCR) {
                        wAPSTaskStep.NCRFlag = 1;
                    }
                    // 三检标记
                    if (wSFCTaskIPTList.stream().anyMatch(p -> p.TaskStepID == wAPSTaskStep.ID)) {
                        wAPSTaskStep.CheckFlag = 1;
                    }
                    // 物料领用单标记
                    if (wPickDemandMap.containsKey(wAPSTaskStep.StepID)) {
                        wAPSTaskStep.PickDemandFlag = wPickDemandMap.get(wAPSTaskStep.StepID);
                    }
                    // 偶换件评审单标记
                    if (wSFCBOMTaskMap.containsKey(wAPSTaskStep.StepID)) {
                        wAPSTaskStep.BOMTaskFlag = wSFCBOMTaskMap.get(wAPSTaskStep.StepID);
                    }

                    List<SFCTaskIPT> wIPTList = wSFCTaskIPTList.stream().filter(p -> p.TaskStepID == wAPSTaskStep.ID)
                            .collect(Collectors.toList());
                    if (wAPSTaskStep.Status == 1) {
                        wAPSTaskStep.StatusText = "未提交";
                    } else if (wAPSTaskStep.Status == 2) {
                        wAPSTaskStep.StatusText = "待开工";
                    } else if (wAPSTaskStep.Status == 4) {
                        wAPSTaskStep.StatusText = "已开工";
                    } else if (wAPSTaskStep.Status == 5) {
                        wAPSTaskStep.StatusText = "已完工";
                    }
                    // 状态
                    if (wAPSTaskStep.Status == APSTaskStatus.Started.getValue()) {
                        if (wIPTList.stream()
                                .anyMatch(p -> p.TaskType == SFCTaskType.SelfCheck.getValue() && p.Status == 1)) {
                            wAPSTaskStep.StatusText = "待自检";
                        } else if (wIPTList.stream()
                                .anyMatch(p -> p.TaskType == SFCTaskType.MutualCheck.getValue() && p.Status == 1)) {
                            wAPSTaskStep.StatusText = "待互检";
                        } else if (wIPTList.stream()
                                .anyMatch(p -> p.TaskType == SFCTaskType.SpecialCheck.getValue() && p.Status == 1)) {
                            wAPSTaskStep.StatusText = "待专检";
                        } else if (wIPTList.stream()
                                .anyMatch(p -> p.TaskType == SFCTaskType.PreCheck.getValue() && p.Status == 1)) {
                            wAPSTaskStep.StatusText = "待预检";
                        }
                    }
                    // 责任班组
                    if (wAPSTaskStep.IsDispatched
                            && wSFCTaskStepList.stream().anyMatch(p -> p.TaskStepID == wAPSTaskStep.ID)) {
                        List<SFCTaskStep> wDisList = wSFCTaskStepList.stream()
                                .filter(p -> p.TaskStepID == wAPSTaskStep.ID).collect(Collectors.toList());
                        int wMonitorID = wDisList.get(0).MonitorID;
                        BMSEmployee wMonitor = APSConstans.GetBMSEmployee(wMonitorID);
                        if (wBMSDepartmentList.stream().anyMatch(p -> p.ID == wMonitor.DepartmentID)) {
                            wAPSTaskStep.DutyDepartment = wBMSDepartmentList.stream()
                                    .filter(p -> p.ID == wMonitor.DepartmentID).findFirst().get().Name;
                            wAPSTaskStep.Monitors = wMonitor.Name;
                        }
                        // 责任人
                        wAPSTaskStep.DutyPersons = StringUtils.Join(",",
                                wDisList.stream().map(p -> p.Operator).collect(Collectors.toList()));
                        // 专检员
                        if (wBMSWorkChargeList.stream().anyMatch(
                                p -> p.ClassID == wMonitor.DepartmentID && p.StationID == wAPSTaskStep.PartID)) {
                            BMSWorkCharge wTemp = wBMSWorkChargeList.stream().filter(
                                            p -> p.ClassID == wMonitor.DepartmentID && p.StationID == wAPSTaskStep.PartID)
                                    .findFirst().get();
                            wAPSTaskStep.CheckerNames = APSUtils.getInstance().GetNames(wTemp.CheckerList);
                        }
                    } else {
                        List<BMSWorkCharge> wChargeList = wBMSWorkChargeList.stream()
                                .filter(p -> p.StationID == wAPSTaskStep.PartID && p.Active == 1)
                                .collect(Collectors.toList());
                        if (wChargeList.size() > 0) {
                            wAPSTaskStep.DutyDepartment = StringUtils.Join(",",
                                    wChargeList.stream().map(p -> p.ClassName).collect(Collectors.toList()));

                            List<BMSEmployee> wMonitorList = APSConstans.GetBMSEmployeeList().values().stream()
                                    .filter(p -> wChargeList.stream().anyMatch(q -> q.ClassID == p.DepartmentID)
                                            && wBMSPositionList.stream().anyMatch(
                                            q -> q.ID == p.Position && q.DutyID == 1)
                                            && p.Active == 1)
                                    .collect(Collectors.toList());
                            if (wMonitorList.size() > 0) {
                                wAPSTaskStep.Monitors = StringUtils.Join(",",
                                        wMonitorList.stream().map(p -> p.Name).distinct().collect(Collectors.toList()));
                            }
                        }
                    }
                    // 工艺师
                    wAPSTaskStep.CraftMans = APSConstans.GetFPCPart(wAPSTaskStep.PartID).TechnicianName;
                    // 专检员
                    if (StringUtils.isEmpty(wAPSTaskStep.CheckerNames)) {
                        wAPSTaskStep.CheckerNames = APSConstans.GetFPCPart(wAPSTaskStep.PartID).CheckerName;
                    }

                    if (wAPSTaskStep.Status == 2 && StringUtils.isEmpty(wAPSTaskStep.DutyPersons)) {
                        wAPSTaskStep.StatusText = "待派工";
                    }
                }

                // 添加进返回结果集
                wResult.Result.addAll(wAPSTaskStepList);
            }

            // 查询工序转序详情
            List<RSMTurnOrderDesc> wRSMTurnOrderDescList = RSMTurnOrderDescDAO.getInstance().SelectList(wLoginUser, -1,
                    wOrderID, -1, -1, -1, wErrorCode);
            wResult.CustomResult.put("RSMTurnOrderDescList", wRSMTurnOrderDescList);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> APS_UpdatePlanDate(BMSEmployee wLoginUser, int wOrderID, int wPartID,
                                                     int wPartPointID, Calendar wPlanStartTime, Calendar wPlanEndTime) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());

            SimpleDateFormat wDateSDF = new SimpleDateFormat("yyyy-MM-dd");

            // 修改工位的计划时间，系统自动修改工序的计划时间
            List<APSTaskPart> wAPSTaskPartList = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, wPartID, 1, APSShiftPeriod.Week.getValue(), null, -1, null, null, wErrorCode);
            if (wPartPointID <= 0) {
                if (wAPSTaskPartList.size() > 0) {
                    APSTaskPart wAPSTaskPart = wAPSTaskPartList.get(0);
                    wAPSTaskPart.StartTime = wPlanStartTime;
                    wAPSTaskPart.EndTime = wPlanEndTime;
                    APSTaskPartDAO.getInstance().Update(wLoginUser, wAPSTaskPart, wErrorCode);
                    // 改工序任务
                    List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, -1, -1,
                            wAPSTaskPart.ID, -1, -1, -1, -1, -1, -1, null, null, null, null, wErrorCode);
                    for (APSTaskStep wAPSTaskStep : wAPSTaskStepList) {
                        wAPSTaskStep.PlanStartTime = wPlanStartTime;
                        wAPSTaskStep.PlanEndTime = wPlanEndTime;
                        APSTaskStepDAO.getInstance().Update(wLoginUser, wAPSTaskStep, wErrorCode);
                    }

                    String wContent = StringUtils.Format("【{0}】在【{1}】将【{2}】-【{3}】的计划日期修改为【{4}-{5}】", wLoginUser.Name,
                            wCurrentTime,
                            wAPSTaskPart.PartNo, wAPSTaskPart.PartName, wDateSDF.format(wPlanStartTime.getTime()),
                            wDateSDF.format(wPlanEndTime.getTime()));

                    LFSOperationLog wLFSOperationLog = new LFSOperationLog(0, wAPSTaskPart.ID,
                            LFSOperationLogType.UpdatePlanDate.getValue(),
                            LFSOperationLogType.UpdatePlanDate.getLable(),
                            wLoginUser.ID, wLoginUser.Name, Calendar.getInstance(), wContent);
                    LFSServiceImpl.getInstance().LFS_UpdateOperationLog(wLoginUser, wLFSOperationLog);
                }
            } else {// 修改工序的计划时间，系统自动修改工位的计划时间
                if (wAPSTaskPartList.size() > 0) {
                    APSTaskPart wAPSTaskPart = wAPSTaskPartList.get(0);
                    // 查询工序任务
                    List<APSTaskStep> wThisTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, -1,
                            -1,
                            wAPSTaskPart.ID, -1, -1, -1, wPartPointID, -1, -1, null, null, null, null, wErrorCode);
                    if (wThisTaskStepList.size() > 0) {
                        APSTaskStep wAPSTaskStep = wThisTaskStepList.get(0);
                        wAPSTaskStep.PlanStartTime = wPlanStartTime;
                        wAPSTaskStep.PlanEndTime = wPlanEndTime;
                        APSTaskStepDAO.getInstance().Update(wLoginUser, wAPSTaskStep, wErrorCode);
                        // 修改工位计划日期
                        List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1,
                                -1, -1,
                                wAPSTaskPart.ID, -1, -1, -1, -1, -1, -1, null, null, null, null, wErrorCode);
                        Calendar wMinTime = wAPSTaskStepList.stream()
                                .min(Comparator.comparing(APSTaskStep::getPlanStartTime)).get().PlanStartTime;
                        Calendar wMaxTime = wAPSTaskStepList.stream()
                                .max(Comparator.comparing(APSTaskStep::getPlanEndTime)).get().PlanEndTime;
                        wAPSTaskPart.StartTime = wMinTime;
                        wAPSTaskPart.EndTime = wMaxTime;
                        APSTaskPartDAO.getInstance().Update(wLoginUser, wAPSTaskPart, wErrorCode);

                        String wContent = StringUtils.Format("【{0}】在【{1}】将【{2}】-【{3}】-【{6}】的计划日期修改为【{4}-{5}】",
                                wLoginUser.Name,
                                wCurrentTime,
                                wAPSTaskPart.PartNo, wAPSTaskPart.PartName, wDateSDF.format(wPlanStartTime.getTime()),
                                wDateSDF.format(wPlanEndTime.getTime()), wAPSTaskStep.StepName);

                        LFSOperationLog wLFSOperationLog = new LFSOperationLog(0, wAPSTaskPart.ID,
                                LFSOperationLogType.UpdatePlanDate.getValue(),
                                LFSOperationLogType.UpdatePlanDate.getLable(),
                                wLoginUser.ID, wLoginUser.Name, Calendar.getInstance(), wContent);
                        LFSServiceImpl.getInstance().LFS_UpdateOperationLog(wLoginUser, wLFSOperationLog);
                    }
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> APS_ComponentAutoFinish(BMSEmployee wLoginUser, String wOrderIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<OMSOrder> wOMSOrderList = OMSOrderDAO.getInstance().SelectListByIDList(wLoginUser,
                    StringUtils.parseIntList(wOrderIDs.split(",")), wErrorCode);
            for (OMSOrder wOMSOrder : wOMSOrderList) {
                if (!(wOMSOrder.PartNo.contains("XS") || wOMSOrder.PartNo.contains("GL"))) {
                    continue;
                }

                String wPartNo = wOMSOrder.PartNo.replace("XS", "").replace("GL", "");

                List<OMSOrder> wNOList = OMSOrderDAO.getInstance().SelectList(wLoginUser, -1, -1, "", -1, -1, -1,
                        wPartNo, "", 1, null, null, null, null, null, wErrorCode);
                if (wNOList.stream().anyMatch(p -> p.Status >= 5)) {
                    wOMSOrder.Status = 5;
                    wOMSOrder.RealFinishDate = wNOList.get(0).RealFinishDate;
                    OMSOrderDAO.getInstance().Update(wLoginUser, wOMSOrder, wErrorCode);
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_CancelAllFinished(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wTaskIDIst = WMSPickDemandDAO.getInstance().WMS_QueryCancelAllFinishedIDList(wLoginUser,
                    wErrorCode);
            for (int wTaskID : wTaskIDIst) {
                WMSServiceImpl.getInstance().WMS_CancelDistribution(wLoginUser, wTaskID);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<APSTaskPart> APS_QueryTaskPart(BMSEmployee wLoginUser, int wOrderID, int wPartID) {
        ServiceResult<APSTaskPart> wResult = new ServiceResult<>();
        wResult.Result = new APSTaskPart();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<APSTaskPart> wAPSTaskPartList = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, wPartID, 1, APSShiftPeriod.Week.getValue(), null, -1, null, null, wErrorCode);
            if (wAPSTaskPartList.size() > 0) {
                wResult.Result = wAPSTaskPartList.get(0);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<APSTaskStep> APS_QueryTaskStep(BMSEmployee wLoginUser, int wOrderID, int wPartID,
                                                        int wPartPointID) {
        ServiceResult<APSTaskStep> wResult = new ServiceResult<>();
        wResult.Result = new APSTaskStep();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, wOrderID, -1,
                    -1, -1, -1, wPartID, wPartPointID, -1, 1, null, null, null, null, wErrorCode);
            if (wAPSTaskStepList.size() > 0) {
                wResult.Result = wAPSTaskStepList.get(0);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_CancelAllFinishedByIDs(BMSEmployee wLoginUser, String wIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wTaskIDIst = StringUtils.parseIntList(wIDs.split(","));
            for (int wTaskID : wTaskIDIst) {
                WMSServiceImpl.getInstance().WMS_CancelDistribution(wLoginUser, wTaskID);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSTaskStep>> QueryOrderBOP(BMSEmployee wLoginUser, int wOrderID,
                                                          int wPartID, int wPartPointID, String wDutyPerson,
                                                          String wCheckName, String wStatusString,
                                                          Pagination wPagination) {
        ServiceResult<List<APSTaskStep>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // List<APSTaskPart> wAPSTaskPartList =
            // APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1,
            // wOrderID, -1,
            // -1, -1, 1,
            // APSShiftPeriod.Week.getValue(), new ArrayList<>(), -1, null, null,
            // wErrorCode);

            // ①获取工位作业班组基础数据
            List<BMSWorkCharge> wBMSWorkChargeList = CoreServiceImpl.getInstance().FMC_QueryWorkChargeList(wLoginUser)
                    .List(BMSWorkCharge.class);
            // ④获取部门列表
            List<BMSDepartment> wBMSDepartmentList = CoreServiceImpl.getInstance()
                    .BMS_QueryDepartmentList(wLoginUser, -1, -1).List(BMSDepartment.class);
            // ⑤获取岗位列表
            List<BMSPosition> wBMSPositionList = CoreServiceImpl.getInstance().BMS_QueryPositionList(wLoginUser, -1)
                    .List(BMSPosition.class);

            List<APSTaskStep> wAllAPSTaskStepList = APSTaskStepDAO.getInstance().SelectListByPage(wLoginUser, -1,
                    wOrderID,
                    -1, -1, -1, -1, wPartID, wPartPointID, -1, 1,
                    null, null, null, null, wDutyPerson, wPagination, wErrorCode);

            List<Integer> wTaskStepIDList = wAllAPSTaskStepList.stream().map(p -> p.ID).distinct()
                    .collect(Collectors.toList());
            List<Integer> wStepIDList = wAllAPSTaskStepList.stream().map(p -> p.StepID).distinct()
                    .collect(Collectors.toList());

            // ②获取派工数据
            List<SFCTaskStep> wSFCTaskStepList = SFCTaskStepDAO.getInstance().SelectListByTaskStepIDList(wLoginUser,
                    wTaskStepIDList, -1, wErrorCode);
            // ③获取检验单数据
            List<SFCTaskIPT> wSFCTaskIPTList = QMSServiceImpl.getInstance()
                    .SFC_QueryTaskIPTListByTaskStepIDList(wLoginUser,
                            wTaskStepIDList)
                    .List(SFCTaskIPT.class);

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2010, 0, 1, 0, 0, 0);

            List<Integer> wPartIDList = wAllAPSTaskStepList.stream().map(p -> p.PartID).distinct()
                    .collect(Collectors.toList());
            Map<Integer, Boolean> wNCRMap = APSTaskStepDAO.getInstance().JudgeIsHasNCR(wLoginUser, wOrderID,
                    wPartIDList, wErrorCode);

            // 物料领用单数据
            Map<Integer, Integer> wPickDemandMap = APSTaskStepDAO.getInstance().WMS_QueryPickDemandMap(wLoginUser,
                    wOrderID, wPartIDList, wStepIDList, wErrorCode);

            // 偶换件评审单数据
            Map<Integer, Integer> wSFCBOMTaskMap = APSTaskStepDAO.getInstance().WMS_QueryBOMTaskMap(wLoginUser,
                    wOrderID, wPartIDList, wStepIDList, wErrorCode);

            // 遍历赋值
            for (APSTaskStep wAPSTaskStep : wAllAPSTaskStepList) {

                // 不合格评审标记
                if (wNCRMap.containsKey(wAPSTaskStep.PartID)) {
                    wAPSTaskStep.NCRFlag = 1;
                }
                // 三检标记
                if (wSFCTaskIPTList.stream().anyMatch(p -> p.TaskStepID == wAPSTaskStep.ID)) {
                    wAPSTaskStep.CheckFlag = 1;
                }
                // 物料领用单标记
                if (wPickDemandMap.containsKey(wAPSTaskStep.StepID)) {
                    wAPSTaskStep.PickDemandFlag = wPickDemandMap.get(wAPSTaskStep.StepID);
                }
                // 偶换件评审单标记
                if (wSFCBOMTaskMap.containsKey(wAPSTaskStep.StepID)) {
                    wAPSTaskStep.BOMTaskFlag = wSFCBOMTaskMap.get(wAPSTaskStep.StepID);
                }

                List<SFCTaskIPT> wIPTList = wSFCTaskIPTList.stream().filter(p -> p.TaskStepID == wAPSTaskStep.ID)
                        .collect(Collectors.toList());
                if (wAPSTaskStep.Status == 1) {
                    wAPSTaskStep.StatusText = "未提交";
                } else if (wAPSTaskStep.Status == 2) {
                    wAPSTaskStep.StatusText = "待开工";
                } else if (wAPSTaskStep.Status == 4) {
                    wAPSTaskStep.StatusText = "已开工";
                } else if (wAPSTaskStep.Status == 5) {
                    wAPSTaskStep.StatusText = "已完工";
                }

                if (wAPSTaskStep.Status == 5 && (wAPSTaskStep.StartTime.compareTo(wBaseTime) <= 0
                        || wAPSTaskStep.EndTime.compareTo(wBaseTime) <= 0)) {
                    wAPSTaskStep.StatusText = "已取消";
                }

                // 状态
                if (wAPSTaskStep.Status == APSTaskStatus.Started.getValue()) {
                    if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.SelfCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待自检";
                    } else if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.MutualCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待互检";
                    } else if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.SpecialCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待专检";
                    } else if (wIPTList.stream()
                            .anyMatch(p -> p.TaskType == SFCTaskType.PreCheck.getValue() && p.Status == 1)) {
                        wAPSTaskStep.StatusText = "待预检";
                    }
                }
                // 责任班组
                if (wAPSTaskStep.IsDispatched
                        && wSFCTaskStepList.stream().anyMatch(p -> p.TaskStepID == wAPSTaskStep.ID)) {
                    List<SFCTaskStep> wDisList = wSFCTaskStepList.stream()
                            .filter(p -> p.TaskStepID == wAPSTaskStep.ID).collect(Collectors.toList());
                    int wMonitorID = wDisList.get(0).MonitorID;
                    BMSEmployee wMonitor = APSConstans.GetBMSEmployee(wMonitorID);
                    if (wBMSDepartmentList.stream().anyMatch(p -> p.ID == wMonitor.DepartmentID)) {
                        wAPSTaskStep.DutyDepartment = wBMSDepartmentList.stream()
                                .filter(p -> p.ID == wMonitor.DepartmentID).findFirst().get().Name;
                        wAPSTaskStep.Monitors = wMonitor.Name;
                    }
                    // 责任人
                    wAPSTaskStep.DutyPersons = StringUtils.Join(",",
                            wDisList.stream().map(p -> p.Operator).collect(Collectors.toList()));
                    // 专检员
                    if (wBMSWorkChargeList.stream().anyMatch(
                            p -> p.ClassID == wMonitor.DepartmentID && p.StationID == wAPSTaskStep.PartID)) {
                        BMSWorkCharge wTemp = wBMSWorkChargeList.stream().filter(
                                        p -> p.ClassID == wMonitor.DepartmentID && p.StationID == wAPSTaskStep.PartID)
                                .findFirst().get();
                        wAPSTaskStep.CheckerNames = APSUtils.getInstance().GetNames(wTemp.CheckerList);
                    }
                } else {
                    List<BMSWorkCharge> wChargeList = wBMSWorkChargeList.stream()
                            .filter(p -> p.StationID == wAPSTaskStep.PartID && p.Active == 1)
                            .collect(Collectors.toList());
                    if (wChargeList.size() > 0) {
                        wAPSTaskStep.DutyDepartment = StringUtils.Join(",",
                                wChargeList.stream().map(p -> p.ClassName).collect(Collectors.toList()));

                        List<BMSEmployee> wMonitorList = APSConstans.GetBMSEmployeeList().values().stream()
                                .filter(p -> wChargeList.stream().anyMatch(q -> q.ClassID == p.DepartmentID)
                                        && wBMSPositionList.stream().anyMatch(
                                        q -> q.ID == p.Position && q.DutyID == 1)
                                        && p.Active == 1)
                                .collect(Collectors.toList());
                        if (wMonitorList.size() > 0) {
                            wAPSTaskStep.Monitors = StringUtils.Join(",",
                                    wMonitorList.stream().map(p -> p.Name).distinct().collect(Collectors.toList()));
                        }
                    }
                }
                // 专检员
                if (StringUtils.isEmpty(wAPSTaskStep.CheckerNames)) {
                    wAPSTaskStep.CheckerNames = APSConstans.GetFPCPart(wAPSTaskStep.PartID).CheckerName;
                }

                if (wAPSTaskStep.Status == 2 && StringUtils.isEmpty(wAPSTaskStep.DutyPersons)) {
                    wAPSTaskStep.StatusText = "待派工";
                }
            }

            // 添加进返回结果集
            wResult.Result.addAll(wAllAPSTaskStepList);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<String> APS_ExportOrderBOP(BMSEmployee wLoginUser, List<APSTaskStep> wAPSTaskStepList) {
        ServiceResult<String> wResult = new ServiceResult<>();
        wResult.Result = "";
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            MyExcelSheet wMyExcelSheet = APSTaskStepDAO.getInstance().GetExportOrderBOPMyExcelSheet(wAPSTaskStepList);

            List<MyExcelSheet> wMyExcelSheetList = new ArrayList<MyExcelSheet>(
                    Collections.singletonList(wMyExcelSheet));

            wResult.Result = ExcelUtil.ExportData(wMyExcelSheetList, wAPSTaskStepList.get(0).PartNo + "_台车BOP");

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_UpdatePickDemand(BMSEmployee wLoginUser, WMSPickDemand wWMSPickDemand) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SynchronizeExpectTime(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<LFSWorkAreaStation> wLFSWorkAreaStationList = LFSServiceImpl.getInstance()
                    .LFS_QueryWorkAreaStationList(wLoginUser).List(LFSWorkAreaStation.class);
            wLFSWorkAreaStationList = wLFSWorkAreaStationList.stream().filter(p -> p.Active == 1)
                    .collect(Collectors.toList());

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());

            List<OMSOrder> wOMSOrderList = OMSOrderDAO.getInstance().SelectList(wLoginUser, null, null, null, null,
                    new ArrayList<>(Collections.singletonList(4)), null, "", null, wErrorCode);
            Calendar wCalendar = Calendar.getInstance();
            int wYear = wCalendar.get(Calendar.YEAR);
            wOMSOrderList = wOMSOrderList.stream().filter(p -> p.Year == wYear && p.OrderType == 1)
                    .collect(Collectors.toList());
            for (OMSOrder wOrder : wOMSOrderList) {
                List<APSTaskPart> apsTaskParts = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOrder.ID,
                        -1, -1, -1, 1, APSShiftPeriod.Week.getValue(), null, -1, null, null, wErrorCode);
                for (APSTaskPart wAPSTaskPart : apsTaskParts) {
                    // 判断是主计划不，若是，则直接查询配送单，判断是否需要同步配送时间
                    if (wLFSWorkAreaStationList.stream()
                            .anyMatch(p -> p.StationID == wAPSTaskPart.PartID && p.PageNumber == 1)) {
                        List<WMSPickDemand> wmsPickDemands = WMSPickDemandDAO.getInstance().SelectListPro(wLoginUser,
                                -1, "100", "",
                                -1, -1, -1, String.valueOf(wOrder.ID), wAPSTaskPart.PartID, -1, null, null, null,
                                1, -1, null, null, Pagination.getMaxSize(), "",
                                "", -1, wErrorCode);
                        for (WMSPickDemand wWMSPickDemand : wmsPickDemands) {
                            if (!wSDF.format(wAPSTaskPart.StartTime.getTime())
                                    .equals(wSDF.format(wWMSPickDemand.ExpectTime1.getTime()))) {
                                wWMSPickDemand.ExpectTime1 = wAPSTaskPart.StartTime;
                                wWMSPickDemand.ExpectTime2 = wAPSTaskPart.StartTime;
                                WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                                // 同步到wms
                                WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wWMSPickDemand.ID,
                                        false);
                            }
                        }
                    } else {
                        // 若不是，则需要查询工序列表，取最小的计划时间，更新周计划的时间，并且更新配送单的时间
                        List<APSTaskStep> apsTaskSteps = APSTaskStepDAO.getInstance().SelectListByTaskPartIDList(
                                wLoginUser,
                                new ArrayList<>(Collections.singletonList(wAPSTaskPart.ID)), wErrorCode);
                        apsTaskSteps = apsTaskSteps.stream().filter(p -> p.Active == 1).collect(Collectors.toList());
                        if (apsTaskSteps.size() <= 0) {
                            continue;
                        }
                        Calendar wMaxTime = apsTaskSteps.stream().max(Comparator.comparing(APSTaskStep::getPlanEndTime))
                                .get().PlanEndTime;
                        Calendar wMinTime = apsTaskSteps.stream()
                                .min(Comparator.comparing(APSTaskStep::getPlanStartTime)).get().PlanStartTime;
                        if (!wSDF.format(wAPSTaskPart.StartTime.getTime()).equals(wSDF.format(wMinTime.getTime()))) {
                            wAPSTaskPart.StartTime = wMinTime;
                            wAPSTaskPart.EndTime = wMaxTime;
                            APSTaskPartDAO.getInstance().Update(wLoginUser, wAPSTaskPart, wErrorCode);
                        }
                        // 同步配送单的计划
                        List<WMSPickDemand> wmsPickDemands = WMSPickDemandDAO.getInstance().SelectListPro(wLoginUser,
                                -1, "100", "",
                                -1, -1, -1, String.valueOf(wOrder.ID), wAPSTaskPart.PartID, -1, null,
                                null, null,
                                1, -1, null, null, Pagination.getMaxSize(), "",
                                "", -1, wErrorCode);
                        for (WMSPickDemand wWMSPickDemand : wmsPickDemands) {
                            if (!wSDF.format(wAPSTaskPart.StartTime.getTime())
                                    .equals(wSDF.format(wWMSPickDemand.ExpectTime1.getTime()))) {
                                wWMSPickDemand.ExpectTime1 = wAPSTaskPart.StartTime;
                                wWMSPickDemand.ExpectTime2 = wAPSTaskPart.StartTime;
                                WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                                // 同步到wms
                                WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wWMSPickDemand.ID,
                                        false);
                            }
                        }
                    }
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("WMS_SynchronizeExpectTime", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SynchronizeExpectTimeByTaskPartList(BMSEmployee wLoginUser,
                                                                          List<APSTaskPart> wAPSTaskPartList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<LFSWorkAreaStation> wLFSWorkAreaStationList = LFSServiceImpl.getInstance()
                    .LFS_QueryWorkAreaStationList(wLoginUser).List(LFSWorkAreaStation.class);
            wLFSWorkAreaStationList = wLFSWorkAreaStationList.stream().filter(p -> p.Active == 1)
                    .collect(Collectors.toList());

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());

            for (APSTaskPart wAPSTaskPart : wAPSTaskPartList) {
                // 判断是主计划不，若是，则直接查询配送单，判断是否需要同步配送时间
                if (wLFSWorkAreaStationList.stream()
                        .anyMatch(p -> p.StationID == wAPSTaskPart.PartID && p.PageNumber == 1)) {
                    List<WMSPickDemand> wmsPickDemands = WMSPickDemandDAO.getInstance().SelectListPro(wLoginUser,
                            -1, "100", "",
                            -1, -1, -1, String.valueOf(wAPSTaskPart.OrderID), wAPSTaskPart.PartID, -1, null, null, null,
                            1, -1, null, null, Pagination.getMaxSize(), "",
                            "", -1, wErrorCode);
                    for (WMSPickDemand wWMSPickDemand : wmsPickDemands) {
                        if (!wSDF.format(wAPSTaskPart.StartTime.getTime())
                                .equals(wSDF.format(wWMSPickDemand.ExpectTime1.getTime()))) {
                            wWMSPickDemand.ExpectTime1 = wAPSTaskPart.StartTime;
                            wWMSPickDemand.ExpectTime2 = wAPSTaskPart.StartTime;
                            WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                            // 此处需要调用wms同步接口，修改预计送达日期
                            WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wWMSPickDemand.ID, false);
                        }
                    }
                    // 同步原单缺料(生产)单
                    wmsPickDemands = WMSPickDemandDAO.getInstance().SelectListPro(wLoginUser,
                            -1, "100", "",
                            -1, -1, -1, String.valueOf(wAPSTaskPart.OrderID), wAPSTaskPart.PartID, -1, null, null, null,
                            9, -1, null, null, Pagination.getMaxSize(), "",
                            "", -1, wErrorCode);
                    for (WMSPickDemand wWMSPickDemand : wmsPickDemands) {
                        if (!wSDF.format(wAPSTaskPart.StartTime.getTime())
                                .equals(wSDF.format(wWMSPickDemand.ExpectTime1.getTime()))) {
                            wWMSPickDemand.ExpectTime1 = wAPSTaskPart.StartTime;
                            wWMSPickDemand.ExpectTime2 = wAPSTaskPart.StartTime;
                            WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                            // 此处需要调用wms同步接口，修改预计送达日期
                            WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wWMSPickDemand.ID, false);
                        }
                    }
                } else {
                    // 若不是，则需要查询工序列表，取最小的计划时间，更新周计划的时间，并且更新配送单的时间
                    List<APSTaskStep> apsTaskSteps = APSTaskStepDAO.getInstance().SelectListByTaskPartIDList(wLoginUser,
                            new ArrayList<>(Collections.singletonList(wAPSTaskPart.ID)), wErrorCode);
                    apsTaskSteps = apsTaskSteps.stream().filter(p -> p.Active == 1).collect(Collectors.toList());
                    if (apsTaskSteps.size() <= 0) {
                        continue;
                    }
                    Calendar wMaxTime = apsTaskSteps.stream().max(Comparator.comparing(APSTaskStep::getPlanEndTime))
                            .get().PlanEndTime;
                    Calendar wMinTime = apsTaskSteps.stream().min(Comparator.comparing(APSTaskStep::getPlanStartTime))
                            .get().PlanStartTime;

                    // 同步配送单的计划
                    List<WMSPickDemand> wmsPickDemands = WMSPickDemandDAO.getInstance().SelectListPro(wLoginUser,
                            -1, "100", "",
                            -1, -1, -1, String.valueOf(wAPSTaskPart.OrderID), wAPSTaskPart.PartID, -1, null,
                            null, null,
                            1, -1, null, null, Pagination.getMaxSize(), "",
                            "", -1, wErrorCode);
                    for (WMSPickDemand wWMSPickDemand : wmsPickDemands) {
                        if (!wSDF.format(wAPSTaskPart.StartTime.getTime())
                                .equals(wSDF.format(wWMSPickDemand.ExpectTime1.getTime()))) {
                            wWMSPickDemand.ExpectTime1 = wAPSTaskPart.StartTime;
                            wWMSPickDemand.ExpectTime2 = wAPSTaskPart.StartTime;
                            WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                            // 此处需要调用wms同步接口，修改预计送达日期
                            WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wWMSPickDemand.ID, false);
                        }
                    }
                    // 同步原单缺料(生产单)
                    wmsPickDemands = WMSPickDemandDAO.getInstance().SelectListPro(wLoginUser,
                            -1, "100", "",
                            -1, -1, -1, String.valueOf(wAPSTaskPart.OrderID), wAPSTaskPart.PartID, -1, null,
                            null, null,
                            9, -1, null, null, Pagination.getMaxSize(), "",
                            "", -1, wErrorCode);
                    for (WMSPickDemand wWMSPickDemand : wmsPickDemands) {
                        if (!wSDF.format(wAPSTaskPart.StartTime.getTime())
                                .equals(wSDF.format(wWMSPickDemand.ExpectTime1.getTime()))) {
                            wWMSPickDemand.ExpectTime1 = wAPSTaskPart.StartTime;
                            wWMSPickDemand.ExpectTime2 = wAPSTaskPart.StartTime;
                            WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);
                            // 此处需要调用wms同步接口，修改预计送达日期
                            WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wWMSPickDemand.ID, false);
                        }
                    }
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<Integer>> WMS_QueryExportIDList(BMSEmployee wLoginUser, String wOrderType,
                                                              String wOrderTypeNot, String wDemandNo, int wProductID,
                                                              int wLineID, int wCustomerID, String wOrderID,
                                                              int wPartID, String wMaterial, int wStatus,
                                                              Calendar wStartTime, Calendar wEndTime, int wSourceType,
                                                              String wSourceTypeNot, int wSendStatus,
                                                              Calendar wPreSendStartDate, Calendar wPreSendEndDate,
                                                              int wIsFinish) {
        ServiceResult<List<Integer>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wIDList = new ArrayList<>();
            if (StringUtils.isNotEmpty(wMaterial)) {
                wIDList = WMSPickDemandItemDAO.getInstance().SelectDemandList(wLoginUser, wMaterial, wStartTime,
                        wEndTime, wErrorCode);
                if (wIDList.size() <= 0) {
                    return wResult;
                }
            }

            wResult.Result = WMSPickDemandDAO.getInstance().SelectExportIDList(wLoginUser, -1, wOrderType, wDemandNo,
                    wProductID, wLineID, wCustomerID, wOrderID, wPartID, wStatus, wStartTime, wEndTime, wIDList,
                    wSourceType, wSendStatus, wPreSendStartDate, wPreSendEndDate, wSourceTypeNot,
                    wOrderTypeNot, wIsFinish,
                    wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SynchronizTimeNotClose(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wTaskIDList = WMSPickDemandDAO.getInstance().SelectNotCloseIDList(wLoginUser, wErrorCode);
            for (int wID : wTaskIDList) {
                WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wID, false);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<FPCRoutePart>> FPC_QueryRoutePartListByRouteID(BMSEmployee wLoginUser, int wRouteID) {
        ServiceResult<List<FPCRoutePart>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = SFCBOMTaskDAO.getInstance().FPC_QueryRoutePartListByRouteID(wLoginUser, wRouteID,
                    wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BFCMessage>> WMS_QueryTechMessageList(BMSEmployee wLoginUser, int wTagType,
                                                                    int wModuleID, Calendar wStartTime,
                                                                    Calendar wEndTime) {
        ServiceResult<List<BFCMessage>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = MESMaterialReturnDAO.getInstance().WMS_QueryTechMessageList(wLoginUser, wTagType,
                    wModuleID, wStartTime, wEndTime, wErrorCode);
            wResult.Result.sort(Comparator.comparing(BFCMessage::getCreateTime, Comparator.reverseOrder()));

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_CompletedClose(BMSEmployee wLoginUser, String wDemandIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wDemandIDList = StringUtils.parseIntList(wDemandIDs.split(","));
            for (int wDemandID : wDemandIDList) {
                WMSPickDemand wWMSPickDemand = WMSPickDemandDAO.getInstance().SelectByID(wLoginUser, wDemandID,
                        wErrorCode);
                WMSPickDemandDAO.getInstance().CompletedClose(wLoginUser, wWMSPickDemand, wErrorCode);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> APS_ResetTaskPart(BMSEmployee wLoginUser, int wTaskPartID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            APSTaskPart apsTaskPart = APSTaskPartDAO.getInstance().SelectByID(wLoginUser, wTaskPartID, wErrorCode);
            if (apsTaskPart == null || apsTaskPart.ID <= 0) {
                return wResult;
            }
            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);

            apsTaskPart.StartWorkTime = wBaseTime;
            apsTaskPart.Status = APSTaskStatus.Issued.getValue();
            APSTaskPartDAO.getInstance().Update(wLoginUser, apsTaskPart, wErrorCode);

            List<APSTaskStep> apsTaskSteps = APSTaskStepDAO.getInstance().SelectListByTaskPartIDList(wLoginUser,
                    new ArrayList<>(Collections.singletonList(apsTaskPart.ID)), wErrorCode);
            if (apsTaskSteps == null || apsTaskSteps.size() <= 0) {
                return wResult;
            }

            for (APSTaskStep apsTaskStep : apsTaskSteps) {
                APSServiceImpl.getInstance().APS_BackNotStart(wLoginUser, apsTaskStep.ID);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<WMSPickDemandItem>> WMS_QueryPickDemandItemListByCondition(BMSEmployee wLoginUser,
                                                                                         int wProductID, int wLineID,
                                                                                         int wCustomerID,
                                                                                         int wOrderID, int wPartID,
                                                                                         int wPartPointID,
                                                                                         String wMaterial,
                                                                                         String wDemandNo,
                                                                                         int wUpdateFlag,
                                                                                         Calendar wStartTime,
                                                                                         Calendar wEndTime,
                                                                                         Pagination wPagination) {
        ServiceResult<List<WMSPickDemandItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = WMSPickDemandItemDAO.getInstance().WMS_QueryPickDemandItemListByCondition(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderID, wPartID, wPartPointID, wMaterial, wDemandNo,
                    wUpdateFlag,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_PickDemandItemReSendFails(BMSEmployee wLoginUser, int wProductID, int wLineID,
                                                                int wCustomerID, int wOrderID, int wPartID,
                                                                int wPartPointID, String wMaterial, String wDemandNo,
                                                                int wUpdateFlag, Calendar wStartTime,
                                                                Calendar wEndTime) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<WMSPickDemandItem> wItemList = WMSPickDemandItemDAO.getInstance()
                    .WMS_QueryPickDemandItemListByConditionNoPage(wLoginUser,
                            wProductID, wLineID, wCustomerID, wOrderID, wPartID, wPartPointID, wMaterial, wDemandNo,
                            wUpdateFlag,
                            wStartTime, wEndTime, wErrorCode);
            if (wItemList.size() <= 0) {
                return wResult;
            }

            List<Integer> wDemandIDList = wItemList.stream().map(p -> p.DemandID).distinct()
                    .collect(Collectors.toList());
            for (int wDemandID : wDemandIDList) {
                List<WMSPickDemandItem> wTempItemList = wItemList.stream().filter(p -> p.DemandID == wDemandID)
                        .collect(Collectors.toList());
                WMSServiceImpl.getInstance().WMS_TestUpdatePickDemand(wLoginUser, wDemandID,
                        StringUtils.Join(",", wTempItemList));
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSBomBPMItem>> APS_ImportItem(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<List<APSBomBPMItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "提示：Excel格式错误!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);
            if (wExcelSheetData.lineData == null || wExcelSheetData.lineData.size() <= 0) {
                wResult.FaultCode += "提示：Sheet格式或内容错误!";
                return wResult;
            }

            for (ExcelLineData wExcelLineData : wExcelSheetData.lineData) {
                if (wExcelLineData.colData == null || wExcelLineData.colData.size() <= 0) {
                    continue;
                }
                if (wExcelLineData.colData.get(0).equals("") || wExcelLineData.colData.get(0).equals("工位")) {
                    continue;
                }

                APSBomBPMItem wAPSBomBPMItem = new APSBomBPMItem();

                // 工位
                wAPSBomBPMItem.PartName = wExcelLineData.colData.get(0);
                wAPSBomBPMItem.PartID = APSConstans.GetFPCPartID(wAPSBomBPMItem.PartName);
                // 工序
                wAPSBomBPMItem.PartPointName = wExcelLineData.colData.get(1);
                wAPSBomBPMItem.PartPointID = APSConstans.GetPartPointIDByName(wAPSBomBPMItem.PartPointName);
                // 物料编码
                wAPSBomBPMItem.MaterialNo = wExcelLineData.colData.get(2);
                if (StringUtils.isNotEmpty(wAPSBomBPMItem.MaterialNo)) {
                    List<MSSMaterial> wMSSMaterialList = WMSServiceImpl.getInstance()
                            .MSS_QueryMaterialList(wLoginUser, wAPSBomBPMItem.MaterialNo).List(MSSMaterial.class);
                    if (wMSSMaterialList != null && wMSSMaterialList.size() > 0) {
                        wAPSBomBPMItem.MaterialID = wMSSMaterialList.get(0).ID;
                        wAPSBomBPMItem.UnitID = wMSSMaterialList.get(0).CYUnitID;
                        wAPSBomBPMItem.UnitText = wMSSMaterialList.get(0).CYUnitText;
                        wAPSBomBPMItem.MaterialName = wMSSMaterialList.get(0).MaterialName;
                    }
                }
                // 数量
                wAPSBomBPMItem.Number = StringUtils.parseDouble(wExcelLineData.colData.get(3));
                // 必换偶换
                wAPSBomBPMItem.ReplaceType = GetReplaceType(wExcelLineData.colData.get(4));
                // 必修偶修
                wAPSBomBPMItem.OutsourceType = GetOutsourceType(wExcelLineData.colData.get(5));
                // 评估类型
                wAPSBomBPMItem.AssessmentType = wExcelLineData.colData.get(6);

                wResult.Result.add(wAPSBomBPMItem);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    private int GetOutsourceType(String wText) {
        int wResult = 0;
        try {
            switch (wText) {
                case "委外必修":
                    wResult = 1;
                    break;
                case "委外偶修":
                    wResult = 2;
                    break;
                case "自修必修":
                    wResult = 3;
                    break;
                case "自修偶修":
                    wResult = 4;
                    break;
                case "其他":
                    wResult = 5;
                    break;
                default:
                    break;
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    private int GetReplaceType(String wText) {
        int wResult = 0;
        try {
            switch (wText) {
                case "必换":
                    wResult = 1;
                    break;
                case "偶换":
                    wResult = 2;
                    break;
                default:
                    break;
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> APS_SynchronizeExpectTime(BMSEmployee wLoginUser, int wAPSBPMID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            APSSchedulingVersionBPM wAPSSchedulingVersionBPM = (APSSchedulingVersionBPM) APSSchedulingVersionBPMDAO
                    .getInstance().BPM_GetTaskInfo(wLoginUser,
                            wAPSBPMID,
                            "", wErrorCode);
            if (wAPSSchedulingVersionBPM == null || wAPSSchedulingVersionBPM.ID <= 0) {
                return wResult;
            }

            List<APSTaskPart> wList = APSTaskPartDAO.getInstance().SelectListByIDList(wLoginUser,
                    wAPSSchedulingVersionBPM.TaskPartIDList,
                    wErrorCode);

            wResult = MyHelperServiceImpl.getInstance().WMS_SynchronizeExpectTimeByTaskPartList(wLoginUser, wList);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SendDemandsByOrderAndPart(BMSEmployee wLoginUser,
                                                                String wOrderIDs, String wPartIDs, String wStepIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            boolean wOutsourcingPlan = Configuration.readConfigString("OutsourcingPlan", "config/config").equals(
                    "1");
            boolean wIsSendRecoilMaterial = Configuration.readConfigString("IsSendRecoilMaterial", "config/config")
                    .equals(
                            "1");

            String[] wOrders = wOrderIDs.split(",");
            String[] wParts = wPartIDs.split(",");
            List<Integer> wStepIDList = StringUtils.parseIntList(wStepIDs.split(","));
            for (String wOrderIDStr : wOrders) {
                int wOrderID = StringUtils.parseInt(wOrderIDStr);
                for (String wPartStr : wParts) {
                    int wPartID = StringUtils.parseInt(wPartStr);

                    // 判断生产计划的物料配送单是否已经创建，若是，则不创建
                    if (StringUtils.isNotEmpty(wStepIDs)) {
                        boolean wFlag = WMSPickDemandDAO.getInstance().JudgeIsCreatedByStepIDs(wLoginUser, wOrderID,
                                wPartID, wStepIDs,
                                wErrorCode);
                        if (wFlag) {
                            continue;
                        }
                    } else {
                        boolean wFlag = WMSPickDemandDAO.getInstance().JudgeIsCreated(wLoginUser, wOrderID, wPartID,
                                wErrorCode);
                        if (wFlag) {
                            continue;
                        }
                    }

                    // 查询台车bom
                    List<APSBOMItem> wList = APSBOMItemDAO.getInstance().APS_QueryBOMItemList(wLoginUser,
                            wOrderID, "", "", -1, -1, -1, wPartID, -1, -1, "", -1, -1, -1, null, -1,
                            -1, -1, wErrorCode);
                    wList = wList.stream().filter(p -> p.ReplaceType == 1).collect(Collectors.toList());
                    if (StringUtils.isNotEmpty(wStepIDs)) {
                        wList = wList.stream().filter(p -> wStepIDList.stream().anyMatch(q -> q == p.PartPointID))
                                .collect(Collectors.toList());
                    }

                    if (wList.size() <= 0) {
                        continue;
                    }

                    // ④创建物料需求计划
                    List<MRPMaterialPlan> wPlanList = new ArrayList<>();

                    // 获取物料类型字典：主料或反冲料
                    List<Integer> wMaterialIDList = wList.stream().map(p -> p.MaterialID).distinct()
                            .collect(Collectors.toList());
                    Map<Integer, Integer> wMaterialTypeMap = MRPMaterialPlanDAO.getInstance().GetMaterialTypeMap(
                            wLoginUser,
                            wMaterialIDList, wErrorCode);

                    for (APSBOMItem apsbomItem : wList) {

                        int wMaterialType = 1;
                        if (wMaterialTypeMap.containsKey(apsbomItem.MaterialID)) {
                            wMaterialType = wMaterialTypeMap.get(apsbomItem.MaterialID);
                        }

                        // 委外和反冲不生成需求计划
                        if (!wOutsourcingPlan && apsbomItem.ReplaceType != 1) {
                            continue;
                        }
                        if (!wIsSendRecoilMaterial && wMaterialType != 1) {
                            continue;
                        }

                        MRPMaterialPlan wMRPMaterialPlan = new MRPMaterialPlan(0, apsbomItem.ProductID,
                                apsbomItem.LineID,
                                apsbomItem.CustomerID, apsbomItem.OrderID, apsbomItem.PartNo, apsbomItem.PartID,
                                apsbomItem.PartPointID, apsbomItem.MaterialID, apsbomItem.MaterialName,
                                apsbomItem.MaterialNo, wMaterialType, apsbomItem.Number, Calendar.getInstance(),
                                "", 1, Calendar.getInstance(), wLoginUser.ID,
                                apsbomItem.ReplaceType, apsbomItem.OutsourceType, 1, apsbomItem.WBSNo,
                                apsbomItem.AssessmentType, apsbomItem.OrderNum, apsbomItem.OriginalType);
                        MRPMaterialPlanDAO.getInstance().Update(wLoginUser, wMRPMaterialPlan, wErrorCode);
                        wPlanList.add(wMRPMaterialPlan);
                    }

                    // ⑤触发必换件物料配送单
                    APSServiceImpl.getInstance().TriggerMaterialDistributionSheetNoCheck(BaseDAO.SysAdmin, wPlanList,
                            MRPMaterialPlanStatus.SystemSubmit.getValue());
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_SendByMaterialNumber(BMSEmployee wLoginUser, int wOrderID, int wPartID,
                                                           int wStepID, int wMaterialID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            boolean wOutsourcingPlan = Configuration.readConfigString("OutsourcingPlan", "config/config").equals(
                    "1");
            boolean wIsSendRecoilMaterial = Configuration.readConfigString("IsSendRecoilMaterial", "config/config")
                    .equals(
                            "1");

            // 查询台车bom
            List<APSBOMItem> wList = APSBOMItemDAO.getInstance().APS_QueryBOMItemList(wLoginUser,
                    wOrderID, "", "", -1, -1, -1, wPartID, wStepID, wMaterialID, "", -1, -1, -1, null, -1,
                    -1, 3, wErrorCode);
            wList = wList.stream().filter(p -> p.ReplaceType == 1).collect(Collectors.toList());

            if (wList.size() <= 0) {
                wResult.FaultCode += "提示：未查询到满足条件的台车bom数据!";
                return wResult;
            }

            // ④创建物料需求计划
            List<MRPMaterialPlan> wPlanList = new ArrayList<>();

            // 获取物料类型字典：主料或反冲料
            List<Integer> wMaterialIDList = wList.stream().map(p -> p.MaterialID).distinct()
                    .collect(Collectors.toList());
            Map<Integer, Integer> wMaterialTypeMap = MRPMaterialPlanDAO.getInstance().GetMaterialTypeMap(wLoginUser,
                    wMaterialIDList, wErrorCode);

            for (APSBOMItem apsbomItem : wList) {

                int wMaterialType = 1;
                if (wMaterialTypeMap.containsKey(apsbomItem.MaterialID)) {
                    wMaterialType = wMaterialTypeMap.get(apsbomItem.MaterialID);
                }

                // 委外和反冲不生成需求计划
                if (!wOutsourcingPlan && apsbomItem.ReplaceType != 1) {
                    continue;
                }
                if (!wIsSendRecoilMaterial && wMaterialType != 1) {
                    continue;
                }

                MRPMaterialPlan wMRPMaterialPlan = new MRPMaterialPlan(0, apsbomItem.ProductID,
                        apsbomItem.LineID,
                        apsbomItem.CustomerID, apsbomItem.OrderID, apsbomItem.PartNo, apsbomItem.PartID,
                        apsbomItem.PartPointID, apsbomItem.MaterialID, apsbomItem.MaterialName,
                        apsbomItem.MaterialNo, wMaterialType, apsbomItem.Number, Calendar.getInstance(),
                        "", 1, Calendar.getInstance(), wLoginUser.ID,
                        apsbomItem.ReplaceType, apsbomItem.OutsourceType, 1, apsbomItem.WBSNo,
                        apsbomItem.AssessmentType, apsbomItem.OrderNum, apsbomItem.OriginalType);
                MRPMaterialPlanDAO.getInstance().Update(wLoginUser, wMRPMaterialPlan, wErrorCode);
                wPlanList.add(wMRPMaterialPlan);
            }

            // ⑤触发必换件物料配送单
            APSServiceImpl.getInstance().TriggerMaterialDistributionSheetNoCheck(BaseDAO.SysAdmin, wPlanList,
                    MRPMaterialPlanStatus.SystemSubmit.getValue());

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public void APS_SynchronizeNotMainTaskPartDate(BMSEmployee wLoginUser, int wOrderID) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<OMSOrder> wOMSOrderList = OMSOrderDAO.getInstance().SelectList(wLoginUser, null, null, null, null,
                    new ArrayList<>(Collections.singletonList(4)), null, "", null, wErrorCode);
            if (wOrderID > 0) {
                wOMSOrderList = wOMSOrderList.stream().filter(p -> p.ID == wOrderID).collect(Collectors.toList());
            }

            // 查询工位排程显示顺序表
            List<LFSWorkAreaStation> wLFSWorkAreaStationList = LFSServiceImpl.getInstance()
                    .LFS_QueryWorkAreaStationList(wLoginUser).List(LFSWorkAreaStation.class);
            wLFSWorkAreaStationList = wLFSWorkAreaStationList.stream().filter(p -> p.Active == 1 && p.PageNumber != 1)
                    .collect(Collectors.toList());
            if (wLFSWorkAreaStationList.size() <= 0) {
                return;
            }

            List<Integer> wStationIDList = wLFSWorkAreaStationList.stream().map(p -> p.StationID).distinct()
                    .collect(Collectors.toList());

            List<APSTaskPart> wAPSTaskPartList;
            List<APSTaskStep> wAPSTaskStepList;
            Calendar wMinPlanStartTime;
            Calendar wMaxPlanEndTime;
            for (OMSOrder wOMSOrder : wOMSOrderList) {
                // ①根据非主计划工位，查询激活的周计划工位任务
                wAPSTaskPartList = APSTaskPartDAO.getInstance().SelectList(wLoginUser, -1, wOMSOrder.ID, -1, -1, -1, 1,
                        APSShiftPeriod.Week.getValue(), null, -1, null, null, wStationIDList,
                        wErrorCode);
                // ②遍历周计划工位任务，查询工序任务集合
                for (APSTaskPart wAPSTaskPart : wAPSTaskPartList) {
                    wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectList(wLoginUser, -1, -1, -1,
                            wAPSTaskPart.ID, -1, -1, -1, -1, -1, 1, null, null, null, null, wErrorCode);
                    if (wAPSTaskStepList == null || wAPSTaskPartList.size() <= 0) {
                        continue;
                    }
                    // ③获取工序任务集合的开始时间最小值

                    Optional<APSTaskStep> min = wAPSTaskStepList.stream()
                            .min(Comparator.comparing(APSTaskStep::getPlanStartTime));

                    if (!min.isPresent()) {
                        continue;
                    }
                    wMinPlanStartTime = min.get().PlanStartTime;
                    // ④获取工序任务集合的结束时间最大值
                    Optional<APSTaskStep> max = wAPSTaskStepList.stream()
                            .max(Comparator.comparing(APSTaskStep::getPlanEndTime));
                    if (!max.isPresent()) {
                        continue;
                    }
                    wMaxPlanEndTime = max.get().PlanEndTime;
                    // ⑤更新周计划工位任务的开始时间和结束时间
                    wAPSTaskPart.StartTime = wMinPlanStartTime;
                    wAPSTaskPart.EndTime = wMaxPlanEndTime;
                    APSTaskPartDAO.getInstance().Update(wLoginUser, wAPSTaskPart, wErrorCode);
                }
            }
        } catch (Exception ex) {
            logger.error("APS_SynchronizeNotMainTaskPartDate", ex);
        }
    }

    @Override
    public ServiceResult<Integer> APS_SynchronizePlanTime(BMSEmployee wLoginUser, int wOrderID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            MyHelperServiceImpl.getInstance().APS_SynchronizeNotMainTaskPartDate(wLoginUser, wOrderID);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_TestSynchronizeSendDate(BMSEmployee wLoginUser, int wTaskPartID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            APSTaskPart wAPSTaskPart = APSTaskPartDAO.getInstance().SelectByID(wLoginUser, wTaskPartID, wErrorCode);
            if (wAPSTaskPart == null || wAPSTaskPart.ID <= 0) {
                wResult.FaultCode += MESException.Parameter.getLable();
                return wResult;
            }

            wResult = MyHelperServiceImpl.getInstance().WMS_SynchronizeExpectTimeByTaskPartList(wLoginUser,
                    new ArrayList<>(Collections.singletonList(wAPSTaskPart)));

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_CancelOutDemand(BMSEmployee wLoginUser, int wDemandID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            WMSPickDemand wWMSPickDemand = WMSServiceImpl.getInstance().WMS_QueryPickDemand(wLoginUser, wDemandID,
                    -1).Result;
            if (wWMSPickDemand == null || wWMSPickDemand.ID <= 0 || !wWMSPickDemand.OrderType.equals("200")) {
                wResult.FaultCode += MESException.Parameter.getLable();
                return wResult;
            }

            if (wWMSPickDemand.Status != WMSPickDemandStatusNew.Saved.getValue()) {
                wResult.FaultCode += "仅可取消`保存`状态的委外部件送修单!";
                return wResult;
            }

            if (wWMSPickDemand.ItemList == null || wWMSPickDemand.ItemList.size() <= 0
                    || wWMSPickDemand.ItemList.stream().anyMatch(p -> p.OutSourceMaterialID <= 0)) {
                wResult.FaultCode += MESException.Logic.getLable();
                return wResult;
            }

            // 修改委外拆修登记物料状态为`保存`
            for (WMSPickDemandItem wWMSPickDemandItem : wWMSPickDemand.ItemList) {
                IPTOutsourcingMaterial wIPTOutsourcingMaterial = QMSServiceImpl.getInstance()
                        .IPT_OutsourcingMaterialInfo(wLoginUser,
                                wWMSPickDemandItem.OutSourceMaterialID)
                        .Info(IPTOutsourcingMaterial.class);
                if (wIPTOutsourcingMaterial == null || wIPTOutsourcingMaterial.ID <= 0) {
                    continue;
                }

                wIPTOutsourcingMaterial.Status = IPTOutsourcingMaterialStatus.Save.getValue();
                QMSServiceImpl.getInstance().IPT_OutsourcingMaterialUpdateData(wLoginUser, wIPTOutsourcingMaterial);
            }

            // 更改委外送修单状态为`取消`
            wWMSPickDemand.Status = WMSPickDemandStatusNew.Canceld.getValue();
            wResult.Result = WMSPickDemandDAO.getInstance().Update(wLoginUser, wWMSPickDemand, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 通过预检或拆解结果，触发委外领料单
     */
    @Override
    public ServiceResult<Integer> WMS_TriggerPickDemandByPrevAndDisa(BMSEmployee wLoginUser, int wOrderID,
                                                                     List<Integer> wPartIDList,
                                                                     List<Integer> wMaterialIDList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①根据订单、物料、预检拆解工位查询物料需求计划
            List<Integer> wPlanIDList = MRPMaterialPlanDAO.getInstance().WMS_QueryPlanIDListByOrderAndMateiral(
                    wLoginUser, wOrderID,
                    wPartIDList, wMaterialIDList, wErrorCode);
            if (wPlanIDList == null || wPlanIDList.size() <= 0) {
                return wResult;
            }
            // ②根据物料需求计划触发领料单
            List<MRPMaterialPlan> wMRPMaterialPlanList = MRPMaterialPlanDAO.getInstance().SelectListByIDList(wLoginUser,
                    wPartIDList, wErrorCode);
            if (wMRPMaterialPlanList == null || wMRPMaterialPlanList.size() <= 0) {
                return wResult;
            }
            // ③根据工位分批触发预检拆解过程的领料单
            List<Integer> wPlanPartIDList = wMRPMaterialPlanList.stream().map(p -> p.PartID).distinct()
                    .collect(Collectors.toList());

            // 读取系统配置，只生成配置的相关工位
            String wOutLLParts = MESSystemConfigDAO.getInstance().SelectValueByKey(wLoginUser,
                    "Out.LL.Parts", wErrorCode);
            List<Integer> wOutLLPartsList = StringUtils.parseIntList(wOutLLParts.split(","));

            for (int wPartID : wPlanPartIDList) {
                List<MRPMaterialPlan> wTriggerPlanList = wMRPMaterialPlanList.stream().filter(p -> p.PartID == wPartID)
                        .collect(Collectors.toList());

                if (wOutLLPartsList.stream().anyMatch(p -> p.intValue() == wPartID)
                        || wOutLLPartsList.size() == 0) {
                    APSServiceImpl.getInstance().TriggerMaterialDistributionSheet_PrevAndDisa(wLoginUser,
                            wTriggerPlanList, MRPMaterialPlanStatus.SystemSubmit.getValue());
                }

            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public void CreateSFCOutMaterialTrackLedgerList(BMSEmployee wLoginUser, WMSPickDemand wPickDemand) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            IPTOutsourcingMaterial wIPTOutsourcingMaterial;
            for (WMSPickDemandItem wWMSPickDemandItem : wPickDemand.ItemList) {
                if (wWMSPickDemandItem.OutSourceMaterialID <= 0) {
                    continue;
                }

                wIPTOutsourcingMaterial = IPTOutsourcingMaterialDAO.getInstance().SelectByID(wLoginUser,
                        wWMSPickDemandItem.OutSourceMaterialID, wErrorCode);
                if (wIPTOutsourcingMaterial == null || wIPTOutsourcingMaterial.ID <= 0) {
                    continue;
                }

                QMSServiceImpl.getInstance().SFC_CreateOutMaterialTrackLedgerList(wLoginUser, wIPTOutsourcingMaterial);
            }
        } catch (Exception ex) {
            logger.error("CreateSFCOutMaterialTrackLedgerList", ex);
        }
    }

    @Override
    public ServiceResult<Integer> WMS_ReRecoilInstockLine(BMSEmployee wLoginUser, int wDemandID, int wDemandItemID,
                                                          int wDemandItemStockID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            WMSPickDemandItem wWMSPickDemandItem = WMSPickDemandItemDAO.getInstance().SelectByID(wLoginUser,
                    wDemandItemID, wErrorCode);
            if (wWMSPickDemandItem == null || wWMSPickDemandItem.ID <= 0) {
                wResult.FaultCode += MESException.Logic.getValue();
                return wResult;
            }

            WMSPickDemandItemStock wWMSPickDemandItemStock = WMSPickDemandItemStockDAO.getInstance()
                    .SelectByID(wLoginUser, wDemandItemStockID, wErrorCode);
            if (wWMSPickDemandItemStock == null || wWMSPickDemandItemStock.ID <= 0) {
                wResult.FaultCode += MESException.Logic.getValue();
                return wResult;
            }

            WMSServiceImpl.getInstance().WMS_IncomingLineSideLibrary_3(wLoginUser, wDemandID,
                    wWMSPickDemandItem, wWMSPickDemandItemStock);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_ReCreateRecoilPickDemand(BMSEmployee wLoginUser, int wDemandID,
                                                               String wDemandItemIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Integer> wDemandItemIDList = StringUtils.parseIntList(wDemandItemIDs.split(","));
            List<WMSPickDemandItem> wItemList = new ArrayList<>();
            for (int wDemandItemID : wDemandItemIDList) {
                WMSPickDemandItem wWMSPickDemandItem = WMSPickDemandItemDAO.getInstance().SelectByID(wLoginUser,
                        wDemandItemID, wErrorCode);
                if (wWMSPickDemandItem != null && wWMSPickDemandItem.ID > 0) {
                    wItemList.add(wWMSPickDemandItem);
                }
            }
            WMSPickDemand wWMSPickDemand = WMSPickDemandDAO.getInstance().SelectByIDWithoutSub(wLoginUser, wDemandID,
                    wErrorCode);
            if (wWMSPickDemand == null || wWMSPickDemand.ID <= 0 || wItemList.size() != wDemandItemIDList.size()) {
                wResult.FaultCode += MESException.DBSQL.getValue();
                return wResult;
            }

            if (wWMSPickDemand.SourceType != WMSPickDemandSourceType.OHJ.getValue()
                    && wWMSPickDemand.SourceType != WMSPickDemandSourceType.Plan.getValue()) {
                wResult.FaultCode += "反冲料只支持重新提报来源为`生产计划`和`偶换件评审`的配送单";
                return wResult;
            }

            if (wWMSPickDemand.SourceType == WMSPickDemandSourceType.OHJ.getValue()) {
                // 查询台车bom
                List<Integer> wOrderNumList = wItemList.stream().map(p -> p.BOMItemNum).collect(Collectors.toList());
                List<Integer> wAPSBOMItemIDList = APSBOMItemDAO.getInstance().SelectIDListByOrderNumList(wLoginUser,
                        wWMSPickDemand.OrderID, wOrderNumList, wErrorCode);
                if (wAPSBOMItemIDList.size() == wOrderNumList.size()) {
                    List<APSBOMItem> wDataList = APSBOMItemDAO.getInstance().SelectByIDList(wLoginUser,
                            wAPSBOMItemIDList, wErrorCode);
                    for (APSBOMItem wAPSBOMItem : wDataList) {
                        wAPSBOMItem.NewSourceID = wWMSPickDemand.SourceID;
                        wAPSBOMItem.NewSourceCode = wWMSPickDemand.SourceCode;
                    }
                    APSServiceImpl.getInstance().CreatePSD(wLoginUser, wDataList, wWMSPickDemand.SourceID, wErrorCode);
                }
            } else {
                List<MRPMaterialPlan> wPlanList = new ArrayList<>();
                for (WMSPickDemandItem wWMSPickDemandItem : wItemList) {
                    List<MRPMaterialPlan> wMRPMaterialPlanList = MRPMaterialPlanDAO.getInstance().SelectList(wLoginUser,
                            -1, -1, -1, -1,
                            wWMSPickDemand.OrderID, wWMSPickDemand.PartID, wWMSPickDemandItem.PartPointID,
                            wWMSPickDemandItem.MaterialID, "", "", -1,
                            "", -1, APSReplaceType.MustChange.getValue(), -1, null, null, null,
                            null, "", wErrorCode);
                    if (wMRPMaterialPlanList != null && wMRPMaterialPlanList.size() > 0) {
                        wPlanList.add(wMRPMaterialPlanList.get(0));
                    }
                }

                if (wPlanList.size() == wItemList.size()) {
                    // 查询物料需求计划
                    APSServiceImpl.getInstance().TriggerMaterialDistributionSheetNoCheck(wLoginUser, wPlanList,
                            MRPMaterialPlanStatus.SystemSubmit.getValue());
                } else {
                    logger.info("物料需求计划查询失败!!!");
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("WMS_ReCreateRecoilPickDemand", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_DeleteCancelledShipping(BMSEmployee wLoginUser, String wDemandIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            String[] split = wDemandIDs.split(",");
            int wDemandID = 0;
            WMSPickDemand wmsPickDemand;
            for (String s : split) {
                wDemandID = StringUtils.parseInt(s);
                if (wDemandID <= 0) {
                    wResult.FaultCode += "配送单ID不能小于或等于0";
                    return wResult;
                }

                wmsPickDemand = WMSPickDemandDAO.getInstance().SelectByIDWithoutSub(wLoginUser,
                        wDemandID, wErrorCode);
                if (wmsPickDemand == null || wmsPickDemand.ID <= 0) {
                    wResult.FaultCode += StringUtils.Format("物料配送单查询失败，ID={0}", wDemandID);
                    return wResult;
                }

                if (wmsPickDemand.Status != WMSPickDemandStatusNew.Canceld.getValue()) {
                    wResult.FaultCode += StringUtils.Format("物料配送单【{0}】状态不是取消", wmsPickDemand.DemandNo);
                    return wResult;
                }

                if (wmsPickDemand.SourceType != WMSPickDemandSourceType.Plan.getValue()) {
                    wResult.FaultCode += StringUtils.Format("物料配送单【{0}】来源不是生产计划", wmsPickDemand.DemandNo);
                    return wResult;
                }

                // 删除配送单
                WMSPickDemandDAO.getInstance().DeleteList(wLoginUser,
                        new ArrayList<>(Collections.singletonList(wmsPickDemand)),
                        wErrorCode);
                // 删除物料需求计划
                MRPMaterialPlanDAO.getInstance().DeleteListByOrderAndPart(wLoginUser, wmsPickDemand.OrderID,
                        wmsPickDemand.PartID, wErrorCode);
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSTaskStep>> APS_QueryTaskStepByTimeNew_V2(BMSEmployee wLoginUser,
                                                                          Calendar wStartTime,
                                                                          Calendar wEndTime, int wAreaID,
                                                                          int wOrderID,
                                                                          Pagination wPagination) {
        ServiceResult<List<APSTaskStep>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // 处理开始时间-结束时间（没给的情况）
            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2010, 0, 1, 0, 0, 0);
            if (wStartTime.compareTo(wBaseTime) <= 0) {
                wStartTime = Calendar.getInstance();
                wStartTime.add(Calendar.YEAR, -3);
            }
            if (wEndTime.compareTo(wBaseTime) <= 0) {
                wEndTime = Calendar.getInstance();
                wEndTime.add(Calendar.DATE, 1);
            }

            // ①分页条件查询周计划ID集合
            List<Integer> wTaskPartIDList = APSTaskPartDAO.getInstance().APS_QueryTaskPartIDListByPage(wLoginUser,
                    wAreaID, wOrderID, wStartTime, wEndTime, wPagination, wErrorCode);
            if (wTaskPartIDList == null || wTaskPartIDList.size() <= 0) {
                return wResult;
            }
            // ②根据周计划ID查询周计划
            List<APSTaskPart> apsTaskParts = APSTaskPartDAO.getInstance().SelectListByIDList(wLoginUser,
                    wTaskPartIDList, wErrorCode);
            if (apsTaskParts == null || apsTaskParts.size() <= 0) {
                return wResult;
            }

            List<APSTaskPart> wTaskPartOrderList = new ArrayList<>();
            for (int wTaskPartID : wTaskPartIDList) {
                Optional<APSTaskPart> first = apsTaskParts.stream().filter(p -> p.ID == wTaskPartID).findFirst();
                first.ifPresent(wTaskPartOrderList::add);
            }

            // ③根据周计划ID查询工序计划
            List<APSTaskStep> apsTaskSteps = APSTaskStepDAO.getInstance().SelectListByTaskPartIDList(wLoginUser,
                    wTaskPartIDList, wErrorCode);
            wResult.Result = apsTaskSteps;
            // ④获取工区工位数据
            List<LFSWorkAreaStation> wLFSWorkAreaStationList = LFSServiceImpl.getInstance()
                    .LFS_QueryWorkAreaStationList(wLoginUser).List(LFSWorkAreaStation.class);
            wLFSWorkAreaStationList = wLFSWorkAreaStationList.stream().filter(p -> p.Active == 1)
                    .collect(Collectors.toList());
            // ④处理数据返回前端
            APSTaskPartDetails wAPSTaskPartDetails;
            LFSWorkAreaStation wLFSWorkAreaStation;
            List<APSTaskStep> wAPSTaskStepList;
            List<APSTaskStep> wFinishList;
            List<APSTaskStep> wStepScheduleList;
            List<APSTaskStep> wStepMakingList;
            List<APSTaskPartDetails> wTaskPartList = new ArrayList<>();
            for (APSTaskPart apsTaskPart : wTaskPartOrderList) {
                Optional<LFSWorkAreaStation> first = wLFSWorkAreaStationList.stream()
                        .filter(p -> p.StationID == apsTaskPart.PartID).findFirst();
                if (!first.isPresent()) {
                    continue;
                }
                wLFSWorkAreaStation = first.get();
                // 筛选工序列表
                wAPSTaskStepList = apsTaskSteps.stream().filter(p -> p.TaskPartID == apsTaskPart.ID && p.Active == 1)
                        .collect(Collectors.toList());
                wFinishList = wAPSTaskStepList.stream().filter(p -> p.Status == APSTaskStatus.Done.getValue())
                        .collect(Collectors.toList());
                wStepScheduleList = wAPSTaskStepList.stream().filter(p -> p.Status == APSTaskStatus.Issued.getValue()
                        || p.Status == APSTaskStatus.Confirm.getValue()
                        || p.Status == APSTaskStatus.Started.getValue()
                        || p.Status == APSTaskStatus.Done.getValue()).collect(Collectors.toList());
                wStepMakingList = wAPSTaskStepList.stream().filter(p -> p.Status == APSTaskStatus.Submit.getValue()
                        || p.Status == APSTaskStatus.ToAudit.getValue()
                        || p.Status == APSTaskStatus.Audited.getValue()).collect(Collectors.toList());

                wAPSTaskPartDetails = new APSTaskPartDetails(apsTaskPart, wAPSTaskStepList.size(), wFinishList.size(),
                        wStepScheduleList.size(),
                        wStepMakingList.size(), wLFSWorkAreaStation.WorkAreaID, wLFSWorkAreaStation.WorkArea,
                        apsTaskPart.PartNo, apsTaskPart.OrderID, apsTaskPart.PartID,
                        apsTaskPart.SubmitTime, wLFSWorkAreaStation.OrderNum);
                wTaskPartList.add(wAPSTaskPartDetails);
            }
            // ⑤返回其他属性OrderList、TaskPartList
            List<Integer> wOrderIDList = apsTaskParts.stream().map(p -> p.OrderID).distinct()
                    .collect(Collectors.toList());
            List<OMSOrder> wOrderList = OMSOrderDAO.getInstance().SelectListByIDList(wLoginUser, wOrderIDList,
                    wErrorCode);
            wResult.CustomResult.put("OrderList", wOrderList);
            wResult.CustomResult.put("TaskPartList", wTaskPartList);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("APS_QueryTaskStepByTimeNew_V2", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<SFCBOMTask> SFC_QueryBOMTaskDetailData(BMSEmployee wLoginUser, int wBOMTaskID, int wStepID) {
        ServiceResult<SFCBOMTask> wResult = new ServiceResult<>();
        wResult.Result = new SFCBOMTask();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①SFCBOMTask/Info
            wResult.Result = SFCServiceImpl.getInstance().SFC_GetBOMTask(wLoginUser, wBOMTaskID).Result;
            if (wResult.Result == null || wResult.Result.ID <= 0) {
                wResult.FaultCode += StringUtils.Format("未查询到偶换件评审单，请检查参数【{0}】是否正确!", wBOMTaskID);
                return wResult;
            }
            // ②FPCProduct/All
            List<FPCProduct> wFPCProductList = new ArrayList<>(APSConstans.GetFPCProductList().values());
            wFPCProductList = wFPCProductList.stream().filter(p -> p.Active == 1).collect(Collectors.toList());
            wResult.CustomResult.put("ProductList", wFPCProductList);
            // ③OMSOrder/StatusAll
            List<OMSOrder> wOMSOrderList = OMSOrderDAO.getInstance().SelectList(wLoginUser, -1, -1, "", -1, -1,
                    -1, "", "", 1, new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7, 9)), null, null, null,
                    null, wErrorCode);
            wResult.CustomResult.put("OrderList", wOMSOrderList);
            // ④FPCPart/All
            List<FPCPart> wFPCPartList = new ArrayList<>(APSConstans.GetFPCPartList().values());
            wFPCPartList = wFPCPartList.stream().filter(p -> p.Active == 1).collect(Collectors.toList());
            wResult.CustomResult.put("PartList", wFPCPartList);
            // ⑤Department/AllDepartment
            List<BMSDepartment> wBMSDepartmentList = new ArrayList<>(APSConstans.GetBMSDepartmentList().values());
            wBMSDepartmentList = wBMSDepartmentList.stream().filter(p -> p.Active == 1).collect(Collectors.toList());
            wResult.CustomResult.put("DepartmentList", wBMSDepartmentList);
            // ⑥SFCBOMTask/StationList
            List<FPCPart> wStationList = SFCServiceImpl.getInstance().SFC_QueryLoginStationList(wLoginUser).Result;
            wResult.CustomResult.put("StationList", wStationList);
            // ⑦History/getBPMActivitiHisTaskByTaskId
            BPMActivitiHisTask wBPMActivitiHisTask = BPMServiceImpl.getInstance().BPM_GetTask(wLoginUser, wStepID)
                    .Info(BPMActivitiHisTask.class);
            wResult.CustomResult.put("HisTask", wBPMActivitiHisTask);
            // ⑧Runtime/getUnfinishedTaskByPIId
            List<BPMActivitiHisTask> wBPMActivitiHisTaskList = BPMServiceImpl.getInstance()
                    .BPM_GetTaskListByInstance(wLoginUser,
                            wResult.Result.FlowID)
                    .List(BPMActivitiHisTask.class);
            wResult.CustomResult.put("UnfinishedTaskList", wBPMActivitiHisTaskList);
            // ⑨/History/getBPMActivitiHisTaskByPIId
            List<BPMActivitiHisTask> wBPMHisTaskList = BPMServiceImpl.getInstance()
                    .BPM_GetHistoryInstanceByID(wLoginUser,
                            wResult.Result.FlowID)
                    .List(BPMActivitiHisTask.class);
            wResult.CustomResult.put("HisTaskList", wBPMActivitiHisTaskList);
            // ⑩User/All
            List<BMSEmployee> wUserList = new ArrayList<>(APSConstans.GetBMSEmployeeList().values());
            wResult.CustomResult.put("UserList", wUserList);
            // ①Repository/getNextSFConditionByTaskId
            List<BPMOperationStep> wBPMOperationStepList = BPMServiceImpl.getInstance()
                    .BPM_GetOperationByTaskID(wLoginUser, wStepID).List(BPMOperationStep.class);
            wResult.CustomResult.put("OperationStepList", wBPMOperationStepList);
            // ②Repository/getFormByPdIdAndActId
            BPMForm wBPMForm = new BPMForm();
            if (wBPMActivitiHisTask != null && StringUtils.isNotEmpty(wBPMActivitiHisTask.ID)) {
                wBPMForm = BPMServiceImpl.getInstance().BPM_GetFormByPdIdAndActId(wLoginUser,
                        wBPMActivitiHisTask.ProcessDefinitionId,
                        wBPMActivitiHisTask.ActivitiID).Info(BPMForm.class);
            }
            wResult.CustomResult.put("BPMForm", wBPMForm);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSBOMItem>> APS_QueryCarBOMByOrderAndStationList(BMSEmployee wLoginUser, int wOrderID,
                                                                                String wStationList) {
        ServiceResult<List<APSBOMItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = APSBOMItemDAO.getInstance().APS_QueryCarBOMByOrderAndStationList(wLoginUser, wOrderID,
                    wStationList, APSReplaceType.Default, APSOutSourceType.WWBX, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("APS_QueryCarBOMByOrderAndStationList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> SFC_AutoConfirmBOMTask(BMSEmployee wLoginUser, int wSFCBomTaskID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①查询SFCBOMTask
            SFCBOMTask wSFCBOMTask = SFCServiceImpl.getInstance().SFC_GetBOMTask(wLoginUser, wSFCBomTaskID).Result;
            if (wSFCBOMTask == null || wSFCBOMTask.ID <= 0) {
                wResult.FaultCode += "未找到对应的偶换件评审单!";
                return wResult;
            }

            // ②判断状态是否包含`确认`，若不是，则返回
            if (!wSFCBOMTask.StatusText.contains("确认")) {
                wResult.FaultCode += "当前偶换件评审单状态不是待确认!";
                return wResult;
            }

            // ③判断SFCBOMTask的编辑时间是否超过一个月，若没超过，则返回
            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.add(Calendar.MONTH, -1);
            if (wSFCBOMTask.SubmitTime.compareTo(wBaseTime) > 0) {
                wResult.FaultCode += "当前偶换件评审单编辑时间未超过一个月!";
                return wResult;
            }

            // ④根据SFCBOMTask的单据编号判断物料配送单是否已完成，若未完成，则返回
            boolean wIsFinished = WMSPickDemandDAO.getInstance().WMS_IsPickDemandFinished(wLoginUser, wSFCBOMTask.Code,
                    wErrorCode);
            if (!wIsFinished) {
                wResult.FaultCode += "当前偶换件评审单对应的物料配送单未完成!";
                return wResult;
            }

            // ⑥执行自动确认逻辑
            wSFCBOMTask.Status = 300;

            List<BPMActivitiHisTask> wNewTaskList = BPMServiceImpl.getInstance()
                    .BPM_GetHistoryInstanceByID(BaseDAO.SysAdmin, wSFCBOMTask.FlowID).List(BPMActivitiHisTask.class);
            wNewTaskList = wNewTaskList.stream().filter(p -> p.Status == 0 && p.Name.contains("确认"))
                    .collect(Collectors.toList());

            APIResult wAPIResult = CoreServiceImpl.getInstance().QMS_CompleteInstance(BaseDAO.SysAdmin, wSFCBOMTask,
                    wNewTaskList.get(0).ID);
            String wMsg = wAPIResult.getMsg();
            if (StringUtils.isNotEmpty(wMsg)) {
                wResult.FaultCode += wMsg;
                return wResult;
            }

            // ⑦还原单据的编辑时间
            SFCBOMTaskDAO.getInstance().UpdateSubmitTime(wLoginUser, wSFCBomTaskID, wSFCBOMTask.SubmitTime, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> SFC_AutoCloseBOMTask(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // 直接查询需要关闭的任务ID列表
            List<Integer> wTaskIDList = SFCBOMTaskDAO.getInstance().SelectToCloseIDList(wLoginUser,
                    wErrorCode);

            if (wTaskIDList.size() <= 0) {
                return wResult;
            }

            // 使用线程池处理大量数据
            int wThreadCount = 10;
            ExecutorService wES = Executors.newFixedThreadPool(wThreadCount);

            // 将任务ID分成多个小批次
            int wBatchSize = (int) Math.ceil(wTaskIDList.size() / (double) wThreadCount);
            List<List<Integer>> wBatchList = new ArrayList<>();

            for (int i = 0; i < wTaskIDList.size(); i += wBatchSize) {
                int wEndIndex = Math.min(i + wBatchSize, wTaskIDList.size());
                wBatchList.add(wTaskIDList.subList(i, wEndIndex));
            }

            // 多线程处理每个批次
            CountDownLatch wLatch = new CountDownLatch(wBatchList.size());
            for (List<Integer> wBatch : wBatchList) {
                wES.submit(() -> {
                    try {
                        for (Integer wTaskID : wBatch) {
                            ServiceResult<Integer> wConfirmResult = this.SFC_AutoConfirmBOMTask(wLoginUser, wTaskID);
                            if (StringUtils.isNotEmpty(wConfirmResult.FaultCode)) {
                                logger.error("Failed to close BOMTask: ID=" + wTaskID + ", Error="
                                        + wConfirmResult.FaultCode);
                            }
                        }
                    } catch (Exception ex) {
                        logger.error("Error in SFC_AutoCloseBOMTask thread: ", ex);
                    } finally {
                        wLatch.countDown();
                    }
                });
            }

            // 等待所有线程完成
            try {
                if (!wLatch.await(10, TimeUnit.MINUTES)) {
                    logger.error("SFC_AutoCloseBOMTask timeout after 10 minutes");
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("SFC_AutoCloseBOMTask interrupted", e);
            }

            // 关闭线程池
            wES.shutdown();

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    @Scheduled(cron = "#{@scheduledConfig.getCronExpression('central.control.send.time')}")
    public void APS_AutoSendPlanToCentralControl() {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①获取中控配置
            String wEnable = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "central.control.enable",
                    wErrorCode);
            if (!wEnable.equals("1")) {
                return;
            }
            // ②获取今日未推送的抱轴箱组装工序计划
            String wPlanStepIDs = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "central.control.plan.stepids", wErrorCode);
            if (StringUtils.isEmpty(wPlanStepIDs)) {
                logger.error("APS_AutoSendPlanToCentralControl:未配置抱轴箱组装工序计划ID列表!");
                return;
            }

            List<Integer> wPlanStepIDList = APSTaskStepDAO.getInstance().APS_QueryPlanStepIDList(BaseDAO.SysAdmin,
                    wPlanStepIDs, wErrorCode);
            if (wPlanStepIDList.size() <= 0) {
                return;
            }
            List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectListByIDList(BaseDAO.SysAdmin,
                    wPlanStepIDList, wErrorCode);
            // ③推送至中控系统
            String wMsg = CentralControl_SendPlan(wAPSTaskStepList);
            // ④更新推送状态
            for (APSTaskStep wAPSTaskStep : wAPSTaskStepList) {
                if (StringUtils.isEmpty(wMsg)) {
                    wAPSTaskStep.TaskLineID = 1;
                    wAPSTaskStep.TaskText = "推送成功";
                    APSTaskStepDAO.getInstance().Update(BaseDAO.SysAdmin, wAPSTaskStep, wErrorCode);
                } else {
                    wAPSTaskStep.TaskLineID = 2;
                    wAPSTaskStep.TaskText = wMsg;
                    APSTaskStepDAO.getInstance().Update(BaseDAO.SysAdmin, wAPSTaskStep, wErrorCode);
                }
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
    }

    /**
     * 推送抱轴箱组装工序计划
     */
    private String CentralControl_SendPlan(List<APSTaskStep> wAPSTaskStepList) {
        String wResult = "暂未推送";
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            String wUrl = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "central.control.send.url", wErrorCode);

            if (StringUtils.isEmpty(wUrl)) {
                logger.error("CentralControl_SendPlan:未配置中控系统推送地址!");
                wResult = "推送失败：未配置中控系统推送地址!";
                return wResult;
            }

            // 产品配置
            List<FPCProduct> wFPCProductList = APSConstans.GetFPCProductList().values().stream()
                    .filter(p -> p.Active == 1)
                    .collect(Collectors.toList());
            // 根据ProductID转为Map
            Map<String, FPCProduct> wFPCProductMap = wFPCProductList.stream()
                    .collect(Collectors.toMap(FPCProduct::getProductNo, p -> p));

            Map<String, Object> wParms = new HashMap<>();

            List<Map<String, Object>> wDataList = new ArrayList<>();
            for (APSTaskStep wAPSTaskStep : wAPSTaskStepList) {
                Map<String, Object> wData = new HashMap<>();
                wData.put("ID", wAPSTaskStep.ID);
                wData.put("OrderNo", wAPSTaskStep.OrderNo);
                wData.put("OrderID", wAPSTaskStep.OrderID);
                wData.put("ProductNo", wAPSTaskStep.ProductNo);
                wData.put("LineName", wAPSTaskStep.LineName);
                wData.put("CustomerName", wAPSTaskStep.CustomerName);
                wData.put("PartNo", wAPSTaskStep.PartNo);
                wData.put("PartID", wAPSTaskStep.PartID);
                wData.put("PartName", wAPSTaskStep.PartName);
                wData.put("StepID", wAPSTaskStep.StepID);
                wData.put("StepName", wAPSTaskStep.StepName);
                wData.put("PlanStartTime", wAPSTaskStep.PlanStartTime);
                wData.put("PlanEndTime", wAPSTaskStep.PlanEndTime);
                if (wFPCProductMap.containsKey(wAPSTaskStep.ProductNo)) {
                    FPCProduct wFPCProduct = wFPCProductMap.get(wAPSTaskStep.ProductNo);
                    wData.put("AxleCount", wFPCProduct.BusinessUnitID);
                } else {
                    logger.error(StringUtils.Format("CentralControl_SendPlan:车型{0}未配置抱轴箱数量!", wAPSTaskStep.ProductNo));
                    wData.put("AxleCount", 0);
                }
                wData.put("Status", wAPSTaskStep.Status);
                wDataList.add(wData);
            }

            wParms.put("dataList", wDataList);

            Map<String, Object> wMap = RemoteInvokeUtils.getInstance().HttpInvoke(wUrl, wParms, HttpMethod.POST,
                    HashMap.class);

            String wLog1 = JSON.toJSONString(wParms);
            String wLog2 = JSON.toJSONString(wMap);
            String wLog = StringUtils.Format("{0}::{1}", wLog1, wLog2);
            CalendarDAO.getInstance().WriteLogFile(wLog, "抱轴箱组装工序计划中控推送", new OMSOrder(), "");

            int wResultCode = StringUtils.parseInt(wMap.get("resultCode"));
            if (wResultCode != 1000) {
                if (wMap.containsKey("msg")) {
                    wResult = wMap.get("msg").toString();
                } else {
                    wResult = "推送失败：未返回推送结果!";
                }
            } else {
                wResult = "";
            }
        } catch (Exception ex) {
            logger.error("CentralControl_SendPlan", ex);
        }
        return wResult;
    }

    /**
     * 推送抱轴箱组装工序计划
     */
    @Override
    public ServiceResult<Integer> APS_SendPlanToCentralControl(BMSEmployee wLoginUser, String wTaskStepIDs) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①参数检查
            if (StringUtils.isEmpty(wTaskStepIDs)) {
                wResult.FaultCode += "工序计划ID不能为空!";
                return wResult;
            }

            // ②获取中控配置
            String wEnable = MESSystemConfigDAO.getInstance().SelectValueByKey(wLoginUser,
                    "central.control.enable", wErrorCode);
            if (!wEnable.equals("1")) {
                wResult.FaultCode += "中控系统未启用!";
                return wResult;
            }

            // ③查询工序计划
            List<Integer> wTaskStepIDList = StringUtils.parseIntList(wTaskStepIDs.split(","));
            List<APSTaskStep> wAPSTaskStepList = APSTaskStepDAO.getInstance().SelectListByIDList(wLoginUser,
                    wTaskStepIDList, wErrorCode);
            if (wAPSTaskStepList.size() <= 0) {
                wResult.FaultCode += "未查询到工序计划数据!";
                return wResult;
            }

            // ③判断工序计划是否存在抱轴箱组装工序计划
            String wPlanStepIDs = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "central.control.plan.stepids", wErrorCode);
            if (StringUtils.isEmpty(wPlanStepIDs)) {
                logger.error("APS_SendPlanToCentralControl:未配置抱轴箱组装工序计划ID列表!");
                return wResult;
            }
            List<Integer> wPlanStepIDList = StringUtils.parseIntList(wPlanStepIDs.split(","));
            // 存在不是抱轴箱组装工序计划的工序计划
            if (wAPSTaskStepList.stream().anyMatch(p -> !wPlanStepIDList.contains(p.StepID))) {
                wResult.FaultCode += "存在非抱轴箱组装工序计划!";
                return wResult;
            }

            // ④推送至中控系统
            String wMsg = CentralControl_SendPlan(wAPSTaskStepList);

            // ⑤更新推送状态
            for (APSTaskStep wAPSTaskStep : wAPSTaskStepList) {
                if (StringUtils.isEmpty(wMsg)) {
                    wAPSTaskStep.TaskLineID = 1;
                    wAPSTaskStep.TaskText = "推送成功";
                    APSTaskStepDAO.getInstance().Update(wLoginUser, wAPSTaskStep, wErrorCode);
                } else {
                    wAPSTaskStep.TaskLineID = 2;
                    wAPSTaskStep.TaskText = wMsg;
                    APSTaskStepDAO.getInstance().Update(wLoginUser, wAPSTaskStep, wErrorCode);
                }
            }

            if (StringUtils.isNotEmpty(wMsg)) {
                wResult.FaultCode += wMsg;
            }

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("APS_SendPlanToCentralControl", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_UpdateWorkSpace(BMSEmployee wLoginUser, String wPartNo, int wWorkSpaceID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①根据车号查询在修的OMSOrder，没有则报错
            OMSOrder wOMSOrder = OMSOrderDAO.getInstance().SelectByPartNo(wLoginUser, wPartNo, wErrorCode);
            if (wOMSOrder == null || wOMSOrder.ID <= 0) {
                wResult.FaultCode += StringUtils.Format("通过车号{0}未查询到在修的项目!", wPartNo);
                return wResult;
            }

            // ②获取配置的工位ID（待更新台位）
            String wStationIDs = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "wms.update.station.ids", wErrorCode);
            if (StringUtils.isEmpty(wStationIDs)) {
                wResult.FaultCode += "未配置待更新台位的工位ID列表!";
                return wResult;
            }
            List<Integer> wStationIDList = StringUtils.parseIntList(wStationIDs.split(","));

            // ③根据订单ID和工位IDs查询配送单
            List<Integer> wDemandIDs = WMSPickDemandDAO.getInstance().SelectIDListByOrderIDAndStationIDList(wLoginUser,
                    wOMSOrder.ID, wStationIDList, wErrorCode);
            if (wDemandIDs.size() <= 0) {
                wResult.FaultCode += StringUtils.Format("通过车号{0}未查询到待更新台位的配送单!", wPartNo);
                return wResult;
            }

            // ④根据配送单ID查询配送单明细
            List<WMSPickDemand> wDemandList = WMSPickDemandDAO.getInstance().SelectListByIDList(wLoginUser, wDemandIDs,
                    wErrorCode);
            if (wDemandList.size() <= 0) {
                wResult.FaultCode += StringUtils.Format("通过车号{0}未查询到待更新台位的配送单!", wPartNo);
                return wResult;
            }

            // ⑤更新配送单的台位ID
            for (WMSPickDemand wDemand : wDemandList) {
                wDemand.WorkSpaceID = wWorkSpaceID;
                WMSPickDemandDAO.getInstance().Update(wLoginUser, wDemand, wErrorCode);
                // ⑥推送给WMS（更新台位，正式测试时打开）
                WMSServiceImpl.getInstance().WMS_SynchronizedToWMS(wLoginUser, wDemand.ID, true);
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("WMS_UpdateWorkSpace", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<APSBomBPMItem>> APS_QueryBomItemList(BMSEmployee wLoginUser, String wCode, int wProductID,
                                                                   int wLineID, int wCustomerID, int wOrderID, String wWBSNo, int wPartID, int wPartPointID,
                                                                   String wMaterialName, String wMaterialNo, int wStatus, int wSAPStatus, int wReplaceType, int wOutSourceType,
                                                                   int wOperationType, Calendar wStartTime, Calendar wEndTime) {
        ServiceResult<List<APSBomBPMItem>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result = APSBomBPMItemDAO.getInstance().APS_QueryBomItemList(wLoginUser, wCode, wProductID, wLineID,
                    wCustomerID, wOrderID, wWBSNo, wPartID, wPartPointID, wMaterialName, wMaterialNo, wStatus,
                    wSAPStatus, wReplaceType, wOutSourceType, wOperationType, wStartTime, wEndTime, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("APS_QueryBomItemList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> WMS_CreateRecoilDemand(BMSEmployee wLoginUser, int wOrderID, int wPartID,
                                                         int wStepID, int wTaskStepID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            // ①读取配置，判断是否需要拆分反冲需求
            String wSplit = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin,
                    "wms.split.recoil.demand", wErrorCode);
            if (StringUtils.isEmpty(wSplit)) {
                wResult.FaultCode += "未配置是否需要拆分反冲需求!";
                return wResult;
            }
            if (!wSplit.equals("1")) {
                logger.info("wms.split.recoil.demand已配置不拆分反冲领料需求");
                return wResult;
            }
            //①判断该工序是否已提反冲的领料需求
            boolean wIsSend = WMSPickDemandDAO.getInstance().IsSendDemandByRecoil(wLoginUser, wOrderID, wStepID, wErrorCode);
            if (wIsSend) {
                logger.info("工序已提反冲领料需求");
                return wResult;
            }
            // ②获取工序计划
            APSTaskStep wAPSTaskStep = APSTaskStepDAO.getInstance().SelectByID(wLoginUser, wTaskStepID, wErrorCode);
            if (wAPSTaskStep == null || wAPSTaskStep.ID <= 0) {
                wResult.FaultCode += "未获取到工序计划，请检查参数TaskStepID是否正确!";
                return wResult;
            }
            // ③根据订单、工位、工序获取反冲台车bom
            List<APSBOMItem> wItemList = APSBOMItemDAO.getInstance().APS_QueryBOMItemListPro(wLoginUser, wOrderID,
                    wPartID, wStepID, 1,
                    2, APSBOMSourceType.StandardBOM.getValue(),
                    wErrorCode);
            if (wItemList == null || wItemList.size() <= 0) {
                wResult.FaultCode += "未查询到反冲标准bom!";
                return wResult;
            }
            // ④创建反冲物料配送单并推送给WMS
            APSServiceImpl.getInstance().CreatePSD_Recol(wLoginUser, wItemList, wAPSTaskStep, wErrorCode);

            wResult.setFaultCode(MESException.getEnumType(wErrorCode.Result).getLable());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error("WMS_CreateRecoilDemand", e);
        }
        return wResult;
    }

}
